From 6d52beca4c9792333bb81828c36eb1387886f102 Mon Sep 17 00:00:00 2001 From: adriana_ardila Date: Sun, 7 Jun 2020 21:55:25 -0500 Subject: [PATCH 1/3] First changes and add new file --- package-lock.json | 13 ++++++++++ package.json | 2 +- src/index.js | 61 +++++++++++++++++++++++++---------------------- 3 files changed, 46 insertions(+), 30 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..dc9b22b 100644 --- a/src/index.js +++ b/src/index.js @@ -1,32 +1,35 @@ -var XMLHttpRequest = require("xmlhttprequest").XMLHttpRequest; +const API = 'https://rickandmortyapi.com/api/character/'; +const xhttp = new XMLHttpRequest(); -var API = 'https://rickandmortyapi.com/api/character/'; -var xhttp = new XMLHttpRequest(); +function fetchData (url_api) { + return new Promise((resolve, reject) => { + xhttp.open("GET", url_api, true); + xhttp.send(); + xhttp.onreadystatechange = function (event) { + if (xhttp.readyState === 4) { + if (xhttp.status == 200) resolve(JSON.parse(xhttp.responseText)); + else { + return reject(`Ha habido un error`); + } + } + }; + }); +} -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); - } - }; - xhttp.open('GET', url_api, false); - xhttp.send(); -}; + 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(`Tercero Llamado...`); + console.log(`Dimensión: ${data3.dimension}`); + }) + .catch((message) => console.log(message)); -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 From 6ccaf8e7bf33ce8d3f24e2ddd3cdb3586f28249d Mon Sep 17 00:00:00 2001 From: adriana_ardila Date: Sun, 7 Jun 2020 21:58:23 -0500 Subject: [PATCH 2/3] template-info --- PULL_REQUEST_TEMPLATE.md | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/PULL_REQUEST_TEMPLATE.md b/PULL_REQUEST_TEMPLATE.md index e85525b..19dca87 100644 --- a/PULL_REQUEST_TEMPLATE.md +++ b/PULL_REQUEST_TEMPLATE.md @@ -1,13 +1,13 @@ ## DESCRIPTION -Nombre: -Usuario Platzi: +Nombre: Adriana Lucia Ardila +Usuario Platzi: @ardila_adri ## Ciudad - [ ] Ciudad de México -- [ ] Bogotá +- [X] Bogotá # Retos: - - [ ] Primer problema - - [ ] Segundo problema - - [ ] Tercer problema + - [X] Primer problema + - [X] Segundo problema + - [X] Tercer problema From 00c34c1991a97424c525ae89aa6db3e9dc8e572b Mon Sep 17 00:00:00 2001 From: adriana_ardila Date: Wed, 30 Sep 2020 23:39:26 -0500 Subject: [PATCH 3/3] Upload files, information and process --- src/index.js | 63 +++++++++++++++++++++++++++------------------------- 1 file changed, 33 insertions(+), 30 deletions(-) diff --git a/src/index.js b/src/index.js index dc9b22b..0032469 100644 --- a/src/index.js +++ b/src/index.js @@ -1,35 +1,38 @@ -const API = 'https://rickandmortyapi.com/api/character/'; +const XMLHttpRequest = require("xmlhttprequest").XMLHttpRequest; + +const API = "https://rickandmortyapi.com/api/character/"; const xhttp = new XMLHttpRequest(); -function fetchData (url_api) { - return new Promise((resolve, reject) => { - xhttp.open("GET", url_api, true); - xhttp.send(); - xhttp.onreadystatechange = function (event) { - if (xhttp.readyState === 4) { - if (xhttp.status == 200) resolve(JSON.parse(xhttp.responseText)); - else { - return reject(`Ha habido un error`); - } +const fetchData = (url_api) => { + xhttp.open("GET", url_api, false); + xhttp.responseType = "json"; + return new Promise((response, reject) => { + xhttp.onreadystatechange = () => { + if (xhttp.readyState === 4) { + if (xhttp.status === 200) { + const res = JSON.parse(xhttp.responseText); + return response(res); + } else { + return reject(new Error("Error has ocurred!")); } - }; - }); -} + } + }; + xhttp.send(); + }); +}; + +const getData = async (url) => { + try { + const first = await fetchData(url); + const second = await fetchData(`${API}${first.results[0].id}`); + const third = await fetchData(second.origin.url); - 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(`Tercero Llamado...`); - console.log(`Dimensión: ${data3.dimension}`); - }) - .catch((message) => console.log(message)); + console.log(`Personajes: ${first.info.count}`); + console.log(`Primer Personaje: ${second.name}`); + console.log(`Dimensión: ${third.dimension}`); + } catch (error) { + console.log(error); + } +}; +getData(API);