From ea42eee6bceffa1d177ff3210fd4b8d181f4c2ed Mon Sep 17 00:00:00 2001 From: Rafnix Guzman Date: Wed, 9 Sep 2020 15:21:05 -0500 Subject: [PATCH 1/4] [FIX] some problems in code --- PULL_REQUEST_TEMPLATE.md | 6 +++--- package-lock.json | 13 +++++++++++++ package.json | 2 +- src/index.js | 38 ++++++++++++++++++++------------------ 4 files changed, 37 insertions(+), 22 deletions(-) create mode 100644 package-lock.json diff --git a/PULL_REQUEST_TEMPLATE.md b/PULL_REQUEST_TEMPLATE.md index e85525b..dc4282a 100644 --- a/PULL_REQUEST_TEMPLATE.md +++ b/PULL_REQUEST_TEMPLATE.md @@ -1,13 +1,13 @@ ## DESCRIPTION -Nombre: -Usuario Platzi: +Nombre: Rafnix Guzmán +Usuario Platzi: rafnixg ## Ciudad - [ ] Ciudad de México - [ ] Bogotá # Retos: - - [ ] Primer problema + - [x] Primer problema - [ ] Segundo problema - [ ] Tercer problema 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..9b8301a 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(); +const API = "https://rickandmortyapi.com/api/character/"; +const xhttp = new XMLHttpRequest(); function fetchData(url_api, callback) { + xhttp.open("GET", url_api, false); xhttp.onreadystatechange = function (event) { - if (xhttp.readyState === '4') { - if (xhttp.status == 200) - callback(null, xhttp.responseText); - else return callback(url_api); + if (xhttp.readyState === 4) { + if (xhttp.status == 200) callback(null, JSON.parse(xhttp.responseText)); + else { + const error = new Error("Error " + url); + return callback(error, null); + } } }; - xhttp.open('GET', url_api, false); xhttp.send(); -}; +} fetchData(API, function (error1, data1) { - if (error1) return console.error('Error' + ' ' + error1); - console.log('Primer Llamado...') + 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...') + if (error2) return console.error(error2); + 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); + 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 +}); From c50872968578f1c54f3f4fa2680edacdd4dd3e22 Mon Sep 17 00:00:00 2001 From: Rafnix Guzman Date: Wed, 9 Sep 2020 15:34:37 -0500 Subject: [PATCH 2/4] [IMP] Change to ES6: Arrow func, Templat Str --- src/index.js | 24 +++++++++++------------- 1 file changed, 11 insertions(+), 13 deletions(-) diff --git a/src/index.js b/src/index.js index 9b8301a..a8b76e7 100644 --- a/src/index.js +++ b/src/index.js @@ -1,24 +1,22 @@ const XMLHttpRequest = require("xmlhttprequest").XMLHttpRequest; const API = "https://rickandmortyapi.com/api/character/"; -const xhttp = new XMLHttpRequest(); +let xhttp = new XMLHttpRequest(); -function fetchData(url_api, callback) { +const fetchData = (url_api, callback) => { xhttp.open("GET", url_api, false); - xhttp.onreadystatechange = function (event) { + xhttp.onreadystatechange = (event) => { if (xhttp.readyState === 4) { - if (xhttp.status == 200) callback(null, JSON.parse(xhttp.responseText)); - else { - const error = new Error("Error " + url); - return callback(error, null); - } + xhttp.status == 200 + ? callback(null, JSON.parse(xhttp.responseText)) + : callback(new Error(url_api), null); } }; xhttp.send(); -} +}; fetchData(API, function (error1, data1) { - if (error1) return console.error("Error" + " " + error1); + if (error1) return console.error(error1); console.log("Primer Llamado..."); fetchData(API + data1.results[0].id, function (error2, data2) { if (error2) return console.error(error2); @@ -26,9 +24,9 @@ fetchData(API, function (error1, data1) { 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); + console.log(`Personajes: ${data1.info.count}`); + console.log(`Primer Personaje: ${data2.name}`); + console.log(`Dimensión: ${data3.dimension}`); }); }); }); From 404bd0af5b040bb99bc3c735216be5f9fda35d3a Mon Sep 17 00:00:00 2001 From: Rafnix Guzman Date: Wed, 9 Sep 2020 15:48:07 -0500 Subject: [PATCH 3/4] [IMP] Change to ES6: Templat Str --- src/index.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/index.js b/src/index.js index a8b76e7..63789d1 100644 --- a/src/index.js +++ b/src/index.js @@ -18,7 +18,7 @@ const fetchData = (url_api, callback) => { fetchData(API, function (error1, data1) { if (error1) return console.error(error1); console.log("Primer Llamado..."); - fetchData(API + data1.results[0].id, function (error2, data2) { + fetchData(`${API}${data1.results[0].id}`, function (error2, data2) { if (error2) return console.error(error2); console.log("Segundo Llamado..."); fetchData(data2.origin.url, function (error3, data3) { @@ -29,4 +29,4 @@ fetchData(API, function (error1, data1) { console.log(`Dimensión: ${data3.dimension}`); }); }); -}); +}); \ No newline at end of file From 8a7d697c4312747a08a18307048230fc97d425ad Mon Sep 17 00:00:00 2001 From: Rafnix Guzman Date: Wed, 9 Sep 2020 15:58:54 -0500 Subject: [PATCH 4/4] [IMP] Change to ES6: Promise --- PULL_REQUEST_TEMPLATE.md | 4 ++-- src/index.js | 51 +++++++++++++++++++++------------------- 2 files changed, 29 insertions(+), 26 deletions(-) diff --git a/PULL_REQUEST_TEMPLATE.md b/PULL_REQUEST_TEMPLATE.md index dc4282a..8b09f49 100644 --- a/PULL_REQUEST_TEMPLATE.md +++ b/PULL_REQUEST_TEMPLATE.md @@ -9,5 +9,5 @@ Usuario Platzi: rafnixg # Retos: - [x] Primer problema - - [ ] Segundo problema - - [ ] Tercer problema + - [X] Segundo problema + - [X] Tercer problema diff --git a/src/index.js b/src/index.js index 63789d1..ae481cc 100644 --- a/src/index.js +++ b/src/index.js @@ -3,30 +3,33 @@ const XMLHttpRequest = require("xmlhttprequest").XMLHttpRequest; const API = "https://rickandmortyapi.com/api/character/"; let xhttp = new XMLHttpRequest(); -const fetchData = (url_api, callback) => { - xhttp.open("GET", url_api, false); - xhttp.onreadystatechange = (event) => { - if (xhttp.readyState === 4) { - xhttp.status == 200 - ? callback(null, JSON.parse(xhttp.responseText)) - : callback(new Error(url_api), null); - } - }; - xhttp.send(); +const fetchData = (url_api) => { + return new Promise((resolve, reject) => { + xhttp.open("GET", url_api, true); + xhttp.onreadystatechange = (event) => { + if (xhttp.readyState === 4) { + xhttp.status == 200 + ? resolve(JSON.parse(xhttp.responseText)) + : reject(new Error(url_api)); + } + }; + xhttp.send(); + }); }; -fetchData(API, function (error1, data1) { - if (error1) return console.error(error1); - console.log("Primer Llamado..."); - fetchData(`${API}${data1.results[0].id}`, function (error2, data2) { - if (error2) return console.error(error2); +fetchData(API) + .then((data) => { + console.log("Primer Llamado..."); + console.log(`Personajes: ${data.info.count}`); + return fetchData(`${API}${data.results[0].id}`); + }) + .then((data) => { 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 + console.log(`Primer Personaje: ${data.name}`); + return fetchData(data.origin.url); + }) + .then((data) => { + console.log("Tercero Llamado..."); + console.log(`Dimensión: ${data.dimension}`); + }) + .catch((erro) => console.error(err));