Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# javascript-challenge
Reto Fundamentos de JavaScript


# Instalación

```
Expand Down
13 changes: 13 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,4 @@
"dependencies": {
"xmlhttprequest": "^1.8.0"
}
}
}
67 changes: 47 additions & 20 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,30 +3,57 @@ var XMLHttpRequest = require("xmlhttprequest").XMLHttpRequest;
var API = 'https://rickandmortyapi.com/api/character/';
var xhttp = new XMLHttpRequest();

function fetchData(url_api, callback) {
xhttp.onreadystatechange = function (event) {
if (xhttp.readyState === '4') {
if (xhttp.status == 200)
callback(null, xhttp.responseText);
else return callback(url_api);
}
};
const fetchData = url_api => {
xhttp.open('GET', url_api, false);
xhttp.send();
};
xhttp.responseType = 'json';
return new Promise((resolve, reject) => {
xhttp.onreadystatechange = () => {
if (xhttp.readyState === 4) {
if (xhttp.status === 200){
const res = JSON.parse(xhttp.responseText)
return resolve(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)



fetchData(API, function (error1, data1) {
if (error1) return console.error('Error' + ' ' + error1);
/* fetchData(API, function (error1, data1) {
if (error1) return console.error(`Error ${error1}`);
console.log('Primer Llamado...')
fetchData(API + data1.results[0].id, function (error2, data2) {
if (error2) return console.error(error1);
fetchData(API + data1.results[0].id, (error2, data2) => {
if (error2) return console.error(`${error1}`);
console.log('Segundo Llamado...')
fetchData(data2.origin.url, function (error3, data3) {
if (error3) return console.error(error3);
console.log('Tercero Llamado...')
console.log('Personajes:' + ' ' + data1.info.count);
console.log('Primer Personaje:' + ' ' + data2.name);
console.log('Dimensión:' + ' ' + data3.dimension);
if (error3) return console.error(`${error3}`);
console.log(`Tercero Llamado...`)
console.log(`Personajes: ${data1.info.count}`);
console.log(`Primer Personaje: ${data2.name}`);
console.log(`Dimensión: ${data3.dimension}`);
});
});
});
}); */