From 925852b00ecaa1fbf05c0e5c39833399326eefb2 Mon Sep 17 00:00:00 2001 From: Alfonso Neil Jimenez Casallas Date: Mon, 13 Jul 2020 18:56:47 -0500 Subject: [PATCH 1/5] =?UTF-8?q?Resolviendo=20errores=20en=20el=20c=C3=B3di?= =?UTF-8?q?go=20fuente=20de=20la=20aplicaci=C3=B3n=20(parte=201)?= 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 | 10 ++++++---- 3 files changed, 20 insertions(+), 5 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..4157de9 100644 --- a/src/index.js +++ b/src/index.js @@ -3,11 +3,13 @@ var XMLHttpRequest = require("xmlhttprequest").XMLHttpRequest; var API = 'https://rickandmortyapi.com/api/character/'; var xhttp = new XMLHttpRequest(); -function fetchData(url_api, callback) { + +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); } }; @@ -29,4 +31,4 @@ fetchData(API, function (error1, data1) { console.log('Dimensión:' + ' ' + data3.dimension); }); }); -}); \ No newline at end of file +}); From 6af0f2d67118641b44a63e9c572e167259c2434e Mon Sep 17 00:00:00 2001 From: Alfonso Neil Jimenez Casallas Date: Mon, 13 Jul 2020 19:59:27 -0500 Subject: [PATCH 2/5] =?UTF-8?q?Resolviendo=20errores=20en=20el=20c=C3=B3di?= =?UTF-8?q?go=20fuente=20de=20la=20aplicaci=C3=B3n=20(parte=202)=20y=20aju?= =?UTF-8?q?stes=20en=20el=20.gitignore?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 4 ++++ PULL_REQUEST_TEMPLATE.md | 8 ++++---- src/index.js | 2 +- 3 files changed, 9 insertions(+), 5 deletions(-) diff --git a/.gitignore b/.gitignore index ad46b30..0406508 100644 --- a/.gitignore +++ b/.gitignore @@ -59,3 +59,7 @@ typings/ # next.js build output .next + +# other +.node-xmlhttprequest-content-5016 +.node-xmlhttprequest-sync-5016 diff --git a/PULL_REQUEST_TEMPLATE.md b/PULL_REQUEST_TEMPLATE.md index e85525b..65ccde5 100644 --- a/PULL_REQUEST_TEMPLATE.md +++ b/PULL_REQUEST_TEMPLATE.md @@ -1,13 +1,13 @@ ## DESCRIPTION -Nombre: -Usuario Platzi: +Nombre: Alfonso Neil Jiménez Casallas +Usuario Platzi: @alfonsoneiljimenez ## Ciudad - [ ] Ciudad de México -- [ ] Bogotá +- [x] Bogotá # Retos: - - [ ] Primer problema + - [x] Primer problema - [ ] Segundo problema - [ ] Tercer problema diff --git a/src/index.js b/src/index.js index 4157de9..c2f747a 100644 --- a/src/index.js +++ b/src/index.js @@ -4,7 +4,7 @@ var API = 'https://rickandmortyapi.com/api/character/'; var xhttp = new XMLHttpRequest(); -function fetchData(url_api, callback) +function fetchData(url_api, callback) { xhttp.onreadystatechange = function (event) { if (xhttp.readyState === 4) { From 75246126618eef050c5115ecc8b586461682e357 Mon Sep 17 00:00:00 2001 From: Alfonso Neil Jimenez Casallas Date: Tue, 14 Jul 2020 12:17:43 -0500 Subject: [PATCH 3/5] =?UTF-8?q?Soluci=C3=B3n=20al=20segundo=20problema=20c?= =?UTF-8?q?on=20ECMAScript=206=20usando=20Arrow=20Functions=20y=20Template?= =?UTF-8?q?=20Strings?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- PULL_REQUEST_TEMPLATE.md | 2 +- src/index.js | 24 ++++++++++++------------ 2 files changed, 13 insertions(+), 13 deletions(-) diff --git a/PULL_REQUEST_TEMPLATE.md b/PULL_REQUEST_TEMPLATE.md index 65ccde5..2b2fd19 100644 --- a/PULL_REQUEST_TEMPLATE.md +++ b/PULL_REQUEST_TEMPLATE.md @@ -9,5 +9,5 @@ Usuario Platzi: @alfonsoneiljimenez # Retos: - [x] Primer problema - - [ ] Segundo problema + - [x] Segundo problema - [ ] Tercer problema diff --git a/src/index.js b/src/index.js index c2f747a..75a62d4 100644 --- a/src/index.js +++ b/src/index.js @@ -4,9 +4,9 @@ var API = 'https://rickandmortyapi.com/api/character/'; var xhttp = new XMLHttpRequest(); -function fetchData(url_api, callback) { +const fetchData =(url_api, callback) => { - xhttp.onreadystatechange = function (event) { + xhttp.onreadystatechange = (event) => { if (xhttp.readyState === 4) { if (xhttp.status == 200) callback(null, JSON.parse(xhttp.responseText)); @@ -15,20 +15,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) { + 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, function (error3, data3) { + 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); + console.log(`Tercero Llamado...`) + console.log(`Personajes: ${data1.info.count}`); + console.log(`Primer Personaje: ${data2.name}`); + console.log(`Dimensión: ${data3.dimension}`); }); }); }); From 7bf1de5860453e8fb1931d794642457483d1d1d9 Mon Sep 17 00:00:00 2001 From: Alfonso Neil Jimenez Casallas Date: Tue, 14 Jul 2020 15:11:04 -0500 Subject: [PATCH 4/5] =?UTF-8?q?Soluci=C3=B3n=20al=20tercer=20problema=20re?= =?UTF-8?q?factorizando=20el=20c=C3=B3digo=20usando=20Promesas=20con=20Ecm?= =?UTF-8?q?aScript=206?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- PULL_REQUEST_TEMPLATE.md | 2 +- src/index.js | 43 ++++++++++++++++++++++++++++++---------- 2 files changed, 33 insertions(+), 12 deletions(-) diff --git a/PULL_REQUEST_TEMPLATE.md b/PULL_REQUEST_TEMPLATE.md index 2b2fd19..bc52128 100644 --- a/PULL_REQUEST_TEMPLATE.md +++ b/PULL_REQUEST_TEMPLATE.md @@ -10,4 +10,4 @@ Usuario Platzi: @alfonsoneiljimenez # Retos: - [x] Primer problema - [x] Segundo problema - - [ ] Tercer problema + - [x] Tercer problema diff --git a/src/index.js b/src/index.js index 75a62d4..2fbeffb 100644 --- a/src/index.js +++ b/src/index.js @@ -1,22 +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/'; +const xhttp = new XMLHttpRequest(); -const fetchData =(url_api, callback) => { - +const fetchData = (url_api) => { + xhttp.responseType = 'json'; + xhttp.open('GET', url_api, true); + xhttp.send(); + return new Promise((resolve, reject) => { xhttp.onreadystatechange = (event) => { if (xhttp.readyState === 4) { - if (xhttp.status == 200) - callback(null, JSON.parse(xhttp.responseText)); - else return callback(url_api); + if (xhttp.status == 200){ + const response = JSON.parse(xhttp.responseText); + resolve(response); + } + else reject(Error(`${url_api} request did not load succesfully`)); } - }; - xhttp.open('GET', url_api, false); - xhttp.send(); + } + }) } +fetchData(`${API}`).then(data1 => { + console.log(`Primer Llamado...`); + console.log(`Personajes: ${data1.info.count}`); + return fetchData(`${API}${data1.results[0].id}`) +}).then(data2 => { + console.log(`Segundo Llamado...`); + console.log(`Primer Personaje: ${data2.name}`); + return fetchData(`${data2.origin.url}`) +}).then(data3 => { + console.log(`Tercer Llamado...`); + console.log(`Dimensión: ${data3.dimension}`); +}).catch(error => { + console.log(error) +}) + +/* fetchData(`${API}`,(error1, data1) => { if (error1) return console.error('Error' + ' ' + error1); console.log(`Primer Llamado...`) @@ -32,3 +52,4 @@ fetchData(`${API}`,(error1, data1) => { }); }); }); +*/ \ No newline at end of file From 5ac426706f1b40760e020384c57760bf6cf7d10a Mon Sep 17 00:00:00 2001 From: Alfonso Neil Jimenez Casallas Date: Tue, 14 Jul 2020 15:15:27 -0500 Subject: [PATCH 5/5] =?UTF-8?q?Eliminando=20c=C3=B3digo=20comentado=20inne?= =?UTF-8?q?cesario?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/index.js | 17 ----------------- 1 file changed, 17 deletions(-) diff --git a/src/index.js b/src/index.js index 2fbeffb..5845091 100644 --- a/src/index.js +++ b/src/index.js @@ -36,20 +36,3 @@ fetchData(`${API}`).then(data1 => { console.log(error) }) -/* -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