From d7b87ee8e3f479572ee8522a5e2107a1a7f1e5fd Mon Sep 17 00:00:00 2001 From: Camiverry1 Date: Fri, 5 Jun 2020 20:11:43 -0500 Subject: [PATCH 1/4] Solucion al reto de JavaScript --- PULL_REQUEST_TEMPLATE.md | 12 +++---- package.json | 2 +- src/index.js | 72 ++++++++++++++++++++++++---------------- 3 files changed, 51 insertions(+), 35 deletions(-) diff --git a/PULL_REQUEST_TEMPLATE.md b/PULL_REQUEST_TEMPLATE.md index e85525b..8a89330 100644 --- a/PULL_REQUEST_TEMPLATE.md +++ b/PULL_REQUEST_TEMPLATE.md @@ -1,13 +1,13 @@ ## DESCRIPTION -Nombre: -Usuario Platzi: +Nombre: Camilo Echeverry +Usuario Platzi: camiverry ## Ciudad - [ ] Ciudad de México -- [ ] Bogotá +- [x] Bogotá # Retos: - - [ ] Primer problema - - [ ] Segundo problema - - [ ] Tercer problema + - [x] Primer problema + - [x] Segundo problema + - [x] Tercer problema 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..b5e4e30 100644 --- a/src/index.js +++ b/src/index.js @@ -1,32 +1,48 @@ -var XMLHttpRequest = require("xmlhttprequest").XMLHttpRequest; +const API = 'https://rickandmortyapi.com/api/character/' -var API = 'https://rickandmortyapi.com/api/character/'; -var xhttp = new XMLHttpRequest(); +//const XMLHttpRequest = require("xmlhttprequest").XMLHttpRequest; +const 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); + +function fetchData(url_api) { + return new Promise((resolve, reject) => { + xhttp.open('GET', url_api, true); + xhttp.send(); + xhttp.onreadystatechange = event => { + if (xhttp.readyState === 4) { + if (xhttp.status == 200) + resolve(JSON.parse(xhttp.responseText)); + else { + return reject(`Ocurrió un error`); + } } }; - xhttp.open('GET', url_api, false); - 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 +}) +} + + +Promise + .all([fetchData(API)]) + .then(data1 => { + + + console.log(`Primer Llamado...`) + console.log(`'Personajes: ${data1[0].info.count}`); + return fetchData(`${API}${data1[0].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(message => console.log(message)) + From 28a36e5514ab63dd8ca49660045aaf9e288c8fd3 Mon Sep 17 00:00:00 2001 From: Camiverry1 Date: Fri, 5 Jun 2020 20:13:10 -0500 Subject: [PATCH 2/4] Se modifica el codigo y se agrega un index.html --- package-lock.json | 13 +++++++++++++ src/index.html | 11 +++++++++++ 2 files changed, 24 insertions(+) create mode 100644 package-lock.json create mode 100644 src/index.html 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/src/index.html b/src/index.html new file mode 100644 index 0000000..882f367 --- /dev/null +++ b/src/index.html @@ -0,0 +1,11 @@ + + + + + + Document + + + + + \ No newline at end of file From 914cf35f434a4e025f3f0190522bcb7745ddc674 Mon Sep 17 00:00:00 2001 From: Camiverry Date: Sun, 7 Jun 2020 17:27:38 -0500 Subject: [PATCH 3/4] Se modifica XMLHttpRequest --- src/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/index.js b/src/index.js index b5e4e30..f551708 100644 --- a/src/index.js +++ b/src/index.js @@ -1,6 +1,6 @@ const API = 'https://rickandmortyapi.com/api/character/' -//const XMLHttpRequest = require("xmlhttprequest").XMLHttpRequest; +const XMLHttpRequest = require("xmlhttprequest").XMLHttpRequest; const xhttp = new XMLHttpRequest(); From b506a96933b7619fc03ede145c0d19ebb993d2e0 Mon Sep 17 00:00:00 2001 From: Camiverry Date: Mon, 8 Jun 2020 12:41:03 -0500 Subject: [PATCH 4/4] Se agregan cambios al codigo --- src/index.js | 22 +++++++++------------- 1 file changed, 9 insertions(+), 13 deletions(-) diff --git a/src/index.js b/src/index.js index f551708..20eed79 100644 --- a/src/index.js +++ b/src/index.js @@ -13,35 +13,31 @@ function fetchData(url_api) { if (xhttp.status == 200) resolve(JSON.parse(xhttp.responseText)); else { - return reject(`Ocurrió un error`); + return reject(`Ocurrió un error, No se pudo descargar la información`); } } }; }) } - Promise .all([fetchData(API)]) - .then(data1 => { - + .then(infoChar1 => { console.log(`Primer Llamado...`) - console.log(`'Personajes: ${data1[0].info.count}`); - return fetchData(`${API}${data1[0].results[0].id}`) - - + console.log(`'Personajes: ${infoChar1[0].info.count}`); + return fetchData(`${API}${infoChar1[0].results[0].id}`) }) -.then(data2 => { +.then(infoChar2 => { console.log(`Segundo Llamado...`) - console.log(`Primer Personaje: ${data2.name}`); - return fetchData(data2.origin.url) + console.log(`Primer Personaje: ${infoChar2.name}`); + return fetchData(infoChar2.origin.url) }) -.then(data3 => { +.then(infoDim => { console.log(`Tercer Llamado...`) - console.log(`Dimensión: ${data3.dimension}`); + console.log(`Dimensión: ${infoDim.dimension}`); }) .catch(message => console.log(message))