From fc3f388c75e78de180a81fc53baa59fcc4d0766e Mon Sep 17 00:00:00 2001 From: Nelson Matos Date: Tue, 9 Jun 2020 09:24:57 -0500 Subject: [PATCH] complete changes --- package.json | 6 +++--- src/index.js | 58 +++++++++++++++++++++++++++------------------------- 2 files changed, 33 insertions(+), 31 deletions(-) diff --git a/package.json b/package.json index 65a8bf2..b347906 100644 --- a/package.json +++ b/package.json @@ -2,9 +2,9 @@ "name": "escuelajs-reto-03", "version": "1.0.0", "description": "Reto 3 Septiembre 14: Curso de Fundamentos de JavaScript", - "main": "index.js", + "main": "src/index.js", "scripts": { - "start": "node src/index.js" + "start:index": "node src/index.js" }, "repository": { "type": "git", @@ -24,4 +24,4 @@ "dependencies": { "xmlhttprequest": "^1.8.0" } -} \ No newline at end of file +} diff --git a/src/index.js b/src/index.js index d6fa599..895667c 100644 --- a/src/index.js +++ b/src/index.js @@ -1,32 +1,34 @@ -var XMLHttpRequest = require("xmlhttprequest").XMLHttpRequest; +const XMLHttpRequest = require(`xmlhttprequest`).XMLHttpRequest -var API = 'https://rickandmortyapi.com/api/character/'; -var xhttp = new XMLHttpRequest(); +let API = 'https://rickandmortyapi.com/api/character/' -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, callback) => { + let xhttp = new XMLHttpRequest(); + xhttp.onreadystatechange = (event) => { + if (xhttp.readyState === 4) { + if (xhttp.status === 200) { + callback(null, JSON.parse(xhttp.responseText)) + } else { + return callback(error, null) + } } - }; - xhttp.open('GET', url_api, false); - xhttp.send(); -}; + } + 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 +fetchData(API, (error1, data1) => { + if (error1) return console.error(`Error ${error1}`) + console.log(`Primer Llamado...`) + fetchData(API + data1.results[0].id, (error2, data2) => { + if (error2) return console.error(error2) + console.log(`Segundo Llamado...`) + fetchData(data2.origin.url, (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