Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JavaScript Challenge fixed #10

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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
12 changes: 6 additions & 6 deletions PULL_REQUEST_TEMPLATE.md
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
## DESCRIPTION

Nombre:
Usuario Platzi:
Nombre: Andrés Felipe Tovar Rojas
Usuario Platzi: andresftovar

## Ciudad
- [ ] Ciudad de México
- [ ] Bogotá
- [x] Bogotá

# Retos:
- [ ] Primer problema
- [ ] Segundo problema
- [ ] Tercer problema
- [x] Primer problema
- [x] Segundo problema
- [x] Tercer problema
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.

5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,6 @@
"homepage": "https://github.com/platzi/escuelajs-reto-03#readme",
"dependencies": {
"xmlhttprequest": "^1.8.0"
}
}
},
"devDependencies": {}
}
63 changes: 34 additions & 29 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,32 +1,37 @@
var XMLHttpRequest = require("xmlhttprequest").XMLHttpRequest;
const API = 'https://rickandmortyapi.com/api/character/';

var API = 'https://rickandmortyapi.com/api/character/';
var xhttp = new XMLHttpRequest();
const XMLHttpRequest = require("xmlhttprequest").XMLHttpRequest;
const 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);
}
};
xhttp.open('GET', url_api, false);
xhttp.send();
};

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);
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);
});
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));