-
Notifications
You must be signed in to change notification settings - Fork 61
Expand file tree
/
Copy pathindex.js
More file actions
43 lines (36 loc) · 1.14 KB
/
index.js
File metadata and controls
43 lines (36 loc) · 1.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
const XMLHttpRequest = require("xmlhttprequest").XMLHttpRequest;
const API = 'https://rickandmortyapi.com/api/character/';
const xhttp = new XMLHttpRequest();
const fetchData = url_api => {
xhttp.open('GET', url_api, false);
xhttp.responseType = 'json';
return new Promise((response, reject) => {
xhttp.onreadystatechange = () => {
if(xhttp.readyState === 4) {
if(xhttp.status === 200) {
const res = JSON.parse(xhttp.responseText);
return response(res);
} else {
return reject(new Error('Error has ocurred!'))
}
}
}
xhttp.send();
})
};
const getData = async url => {
try {
const first = await fetchData(url);
// console.log('Primer llamado');
const second = await fetchData(`${API}${first.results[0].id}`);
// console.log('Segundo llamado');
const third = await fetchData(second.origin.url);
// console.log('Tercer llamado');
console.log(`Personajes: ${first.info.count}`);
console.log(`Primer personaje: ${second.name}`);
console.log(`Dimensión: ${third.dimension}`);
} catch(error) {
console.log(error)
}
}
getData(API);