From af612ab2caac3875b84b96e82b3e30ff5c3610e0 Mon Sep 17 00:00:00 2001 From: john1199 Date: Mon, 21 Sep 2020 21:33:20 -0500 Subject: [PATCH 1/3] solution first problem --- PULL_REQUEST_TEMPLATE.md | 6 +++--- package-lock.json | 13 +++++++++++++ package.json | 2 +- src/index.js | 33 +++++++++++++++++---------------- 4 files changed, 34 insertions(+), 20 deletions(-) create mode 100644 package-lock.json diff --git a/PULL_REQUEST_TEMPLATE.md b/PULL_REQUEST_TEMPLATE.md index e85525b..1864d9c 100644 --- a/PULL_REQUEST_TEMPLATE.md +++ b/PULL_REQUEST_TEMPLATE.md @@ -1,13 +1,13 @@ ## DESCRIPTION Nombre: -Usuario Platzi: +Usuario Platzi: jhon091199@gmail.com ## Ciudad - [ ] Ciudad de México -- [ ] Bogotá +- [x] 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..76a111f 100644 --- a/src/index.js +++ b/src/index.js @@ -1,32 +1,33 @@ var XMLHttpRequest = require("xmlhttprequest").XMLHttpRequest; -var API = 'https://rickandmortyapi.com/api/character/'; +var API = "https://rickandmortyapi.com/api/character/"; var 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); + if (xhttp.readyState === 4) { + if (xhttp.status == 200) { + var myJson = JSON.parse(xhttp.responseText); + callback(null, myJson); + }else return callback(url_api); } }; - xhttp.open('GET', url_api, false); + 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 786a259197022a89de92b2b42ed79833d808dcb4 Mon Sep 17 00:00:00 2001 From: john1199 Date: Mon, 21 Sep 2020 22:03:26 -0500 Subject: [PATCH 2/3] second problem-solution --- PULL_REQUEST_TEMPLATE.md | 2 +- src/index.js | 23 +++++++++++++---------- 2 files changed, 14 insertions(+), 11 deletions(-) diff --git a/PULL_REQUEST_TEMPLATE.md b/PULL_REQUEST_TEMPLATE.md index 1864d9c..c1abf8c 100644 --- a/PULL_REQUEST_TEMPLATE.md +++ b/PULL_REQUEST_TEMPLATE.md @@ -9,5 +9,5 @@ Usuario Platzi: jhon091199@gmail.com # Retos: - [x] Primer problema - - [ ] Segundo problema + - [x] Segundo problema - [ ] Tercer problema diff --git a/src/index.js b/src/index.js index 76a111f..e181bc4 100644 --- a/src/index.js +++ b/src/index.js @@ -3,31 +3,34 @@ 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) { var myJson = JSON.parse(xhttp.responseText); callback(null, myJson); - }else return callback(url_api); + } else return callback(url_api); } }; 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(error2); + 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}`); }); }); }); From e3306a2f20c62558d56e8d20dbdf245f794321f3 Mon Sep 17 00:00:00 2001 From: john1199 Date: Tue, 22 Sep 2020 00:25:33 -0500 Subject: [PATCH 3/3] third problem-solution --- PULL_REQUEST_TEMPLATE.md | 2 +- src/index.js | 56 +++++++++++++++++++++++----------------- 2 files changed, 33 insertions(+), 25 deletions(-) diff --git a/PULL_REQUEST_TEMPLATE.md b/PULL_REQUEST_TEMPLATE.md index c1abf8c..9447d17 100644 --- a/PULL_REQUEST_TEMPLATE.md +++ b/PULL_REQUEST_TEMPLATE.md @@ -10,4 +10,4 @@ Usuario Platzi: jhon091199@gmail.com # Retos: - [x] Primer problema - [x] Segundo problema - - [ ] Tercer problema + - [x] Tercer problema diff --git a/src/index.js b/src/index.js index e181bc4..eafe778 100644 --- a/src/index.js +++ b/src/index.js @@ -3,34 +3,42 @@ 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) { - var myJson = JSON.parse(xhttp.responseText); - callback(null, myJson); - } else return callback(url_api); - } - }; - xhttp.open("GET", url_api, false); - xhttp.send(); +const fetchData = (url_api) => { + return new Promise((resolve, reject) => { + xhttp.open("GET", url_api, false); + xhttp.onreadystatechange = (event) => { + if (xhttp.readyState === 4 && xhttp.status == 200) + resolve(JSON.parse(xhttp.responseText)); + else reject(); + }; + xhttp.send(); + }); }; -fetchData(API, (error1, data1) => { - if (error1) return console.error("Error" + " " + error1); +fetchData(API) + .then((data1) => { + console.log("Primer Llamado..."); - console.log("Primer Llamado..."); - fetchData(API + data1.results[0].id, (error2, data2) => { - if (error2) return console.error(error2); + setTimeout(() => { + console.log(`Personajes: ${data1.info.count}`); + }, 1000); + return fetchData(API + data1.results[0].id); + }) + .then((data2) => { 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}`); + + setTimeout(() => { console.log(`Primer Personaje: ${data2.name}`); + }, 1000); + + return fetchData(data2.origin.url); + }) + .then((data3) => { + console.log("Tercer Llamado..."); + + setTimeout(() => { console.log(`Dimensión: ${data3.dimension}`); - }); - }); -}); + }, 1000); + }) + .catch((err) => console.log("Error! " + err));