From 4e318cb01f4fbd91096358f66718881c3e84bb2d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sebasti=C3=A1n=20Carabali?= Date: Mon, 29 Jun 2020 12:29:52 -0500 Subject: [PATCH 1/3] =?UTF-8?q?Se=20solucionan=20los=20errores=20que=20no?= =?UTF-8?q?=20permiten=20ejecutar=20la=20aplicaci=C3=B3n?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- package-lock.json | 13 +++++++++++++ package.json | 2 +- src/index.js | 4 ++-- 3 files changed, 16 insertions(+), 3 deletions(-) create mode 100644 package-lock.json diff --git a/package-lock.json b/package-lock.json new file mode 100644 index 0000000..e8c8dbc --- /dev/null +++ b/package-lock.json @@ -0,0 +1,13 @@ +{ + "name": "escuelajs-reto-03", + "version": "1.0.0", + "lockfileVersion": 1, + "requires": true, + "dependencies": { + "xmlhttprequest": { + "version": "1.8.0", + "resolved": "https://registry.npmjs.org/xmlhttprequest/-/xmlhttprequest-1.8.0.tgz", + "integrity": "sha1-Z/4HXFwk/vOfnWX197f+dRcZaPw=" + } + } +} diff --git a/package.json b/package.json index 65a8bf2..dbaa2f1 100644 --- a/package.json +++ b/package.json @@ -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..701e36b 100644 --- a/src/index.js +++ b/src/index.js @@ -5,9 +5,9 @@ var xhttp = new XMLHttpRequest(); function fetchData(url_api, callback) { xhttp.onreadystatechange = function (event) { - if (xhttp.readyState === '4') { + if (xhttp.readyState === 4) { if (xhttp.status == 200) - callback(null, xhttp.responseText); + callback(null, JSON.parse(xhttp.responseText)); else return callback(url_api); } }; From 65795165512676db2045d1f6f096f400ea0842b2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sebasti=C3=A1n=20Carabali?= Date: Mon, 29 Jun 2020 12:54:34 -0500 Subject: [PATCH 2/3] =?UTF-8?q?Se=20convierte=20el=20c=C3=B3digo=20a=20ES&?= =?UTF-8?q?.=20Se=20usan=20arrow=20function=20y=20template=20string?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/index.js | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/src/index.js b/src/index.js index 701e36b..6ad92cb 100644 --- a/src/index.js +++ b/src/index.js @@ -3,8 +3,8 @@ 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) { +const fetchData = (url_api, callback) => { + xhttp.onreadystatechange = (event) => { if (xhttp.readyState === 4) { if (xhttp.status == 200) callback(null, JSON.parse(xhttp.responseText)); @@ -13,20 +13,20 @@ function fetchData(url_api, callback) { }; xhttp.open('GET', url_api, false); xhttp.send(); -}; +} -fetchData(API, function (error1, data1) { +fetchData(API, (error1, data1) => { if (error1) return console.error('Error' + ' ' + error1); console.log('Primer Llamado...') - fetchData(API + data1.results[0].id, function (error2, data2) { + 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) { + 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); + console.log(`Personajes: ${data1.info.count}`); + console.log(`Primer Personaje: ${data2.name}`); + console.log(`Dimensión: ${data3.dimension}`); }); }); }); \ No newline at end of file From 64e8854519470a61a31201afc6b4279d06ab0b0c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sebasti=C3=A1n=20Carabali?= Date: Mon, 29 Jun 2020 13:33:16 -0500 Subject: [PATCH 3/3] Se evita el callback hell haciendo uso de promesas --- src/index.js | 47 +++++++++++++++++++++++------------------------ 1 file changed, 23 insertions(+), 24 deletions(-) diff --git a/src/index.js b/src/index.js index 6ad92cb..fd0337e 100644 --- a/src/index.js +++ b/src/index.js @@ -3,30 +3,29 @@ var XMLHttpRequest = require("xmlhttprequest").XMLHttpRequest; var API = 'https://rickandmortyapi.com/api/character/'; var xhttp = new XMLHttpRequest(); -const fetchData = (url_api, callback) => { - xhttp.onreadystatechange = (event) => { - if (xhttp.readyState === 4) { - if (xhttp.status == 200) - callback(null, JSON.parse(xhttp.responseText)); - else return callback(url_api); +const fetchData = (url_api) => { + return new Promise((resolve, reject) => { + xhttp.onreadystatechange = (event) => { + if (xhttp.readyState === 4) { + if (xhttp.status == 200) + resolve(JSON.parse(xhttp.responseText)) + else + reject(url_api) + } } - }; - xhttp.open('GET', url_api, false); - xhttp.send(); + xhttp.open('GET', url_api, false) + xhttp.send() + }) } -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(error1); - 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 +fetchData(API).then(data => { + console.log(`Personajes: ${data.info.count}`) + return fetchData(`${API}${data.results[0].id}`) +}).then(primerPersonaje => { + console.log(`Primer Personaje: ${primerPersonaje.name}`) + return fetchData(primerPersonaje.origin.url); +}).then(origen => { + console.log(`Dimensión: ${origen.dimension}`) +}).catch(error => { + console.log(error) +}) \ No newline at end of file