-
Notifications
You must be signed in to change notification settings - Fork 61
Expand file tree
/
Copy pathindex.js
More file actions
34 lines (32 loc) · 1012 Bytes
/
index.js
File metadata and controls
34 lines (32 loc) · 1012 Bytes
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
const API = "https://rickandmortyapi.com/api/character/";
const xhttp = new XMLHttpRequest();
function fetchData(url_api) {
return new Promise((resolve, reject) => {
xhttp.open("GET", url_api, true);
xhttp.send();
xhttp.onreadystatechange = (event) => {
if (xhttp.readyState === 4) {
if (xhttp.status == 200) resolve(JSON.parse(xhttp.responseText));
else {
return reject(`ha ocurrido un error inesperado`);
}
}
};
});
}
Promise.all([fetchData(API)])
.then((data1) => {
console.log(`Primer Llamado...`);
console.log(`'Personajes: ${data1[0].info.count}`);
return fetchData(`${API}${data1[0].results[0].id}`);
})
.then((data2) => {
console.log(`Segundo Llamado...`);
console.log(`Primer Personaje: ${data2.name}`);
return fetchData(data2.origin.url);
})
.then((data3) => {
console.log(`Tercer Llamado...`);
console.log(`Dimensión: ${data3.dimension}`);
})
.catch((message) => console.log(message));