diff --git a/PULL_REQUEST_TEMPLATE.md b/PULL_REQUEST_TEMPLATE.md index e85525b..87ee04f 100644 --- a/PULL_REQUEST_TEMPLATE.md +++ b/PULL_REQUEST_TEMPLATE.md @@ -1,13 +1,13 @@ ## DESCRIPTION -Nombre: -Usuario Platzi: +Nombre: María Angélica Pérez Gutiérrez +Usuario Platzi: Maria_PerezG ## Ciudad - [ ] Ciudad de México -- [ ] Bogotá +- [x] Bogotá # Retos: - - [ ] Primer problema - - [ ] Segundo problema - - [ ] Tercer problema + - [Done] Primer problema + - [Done] Segundo problema + - [Done] Tercer problema diff --git a/src/index.js b/src/index.js index d6fa599..58f2e3a 100644 --- a/src/index.js +++ b/src/index.js @@ -1,32 +1,42 @@ -var XMLHttpRequest = require("xmlhttprequest").XMLHttpRequest; +const XMLHttpRequest = require("xmlhttprequest").XMLHttpRequest; -var API = 'https://rickandmortyapi.com/api/character/'; -var xhttp = new XMLHttpRequest(); +const API = 'https://rickandmortyapi.com/api/character/'; -function fetchData(url_api, callback) { - xhttp.onreadystatechange = function (event) { - if (xhttp.readyState === '4') { +function fetchData(url_api) { + let xhttp = new XMLHttpRequest(); + return new Promise ((resolve, reject)=>{ + xhttp.onreadystatechange = event=> { + if (xhttp.readyState == 4) { if (xhttp.status == 200) - callback(null, xhttp.responseText); - else return callback(url_api); - } + resolve(JSON.parse(xhttp.responseText)); + else return reject(url_api); + }; }; - xhttp.open('GET', url_api, false); + xhttp.open('GET', url_api,true); 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); - }); - }); -}); \ No newline at end of file +function onerror(url_api){ + console.log(`sucedió un error :(`); +} + +fetchData(API) + .then((data)=> { + data1=data; + console.log(`Primer Llamado...`); + return fetchData(`${API}${data.results[0].id}`); + }) + .then((data)=> { + data2=data; + console.log(`Segundo Llamado...`); + return fetchData(`${data.origin.url}`); + }) + .then((data)=> { + data3=data; + console.log(`Tercer Llamado...`); + console.log(`Personajes: ${data1.info.count}`); + console.log(`Primer Personaje:${data2.name}`); + console.log(`Dimensión: ${data3.dimension}`); + }) + .catch(onerror); \ No newline at end of file