From 2d838fd87391f723763c8c6a20d11497c859d6e1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mar=C3=ADa=20P=C3=A9rez?= Date: Mon, 1 Jun 2020 22:16:53 -0500 Subject: [PATCH 1/4] fail 1 --- src/index.js | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/src/index.js b/src/index.js index d6fa599..870a588 100644 --- a/src/index.js +++ b/src/index.js @@ -1,30 +1,30 @@ -var XMLHttpRequest = require("xmlhttprequest").XMLHttpRequest; +let XMLHttpRequest = require("xmlhttprequest").XMLHttpRequest; -var API = 'https://rickandmortyapi.com/api/character/'; -var xhttp = new XMLHttpRequest(); +const API = 'https://rickandmortyapi.com/api/character/'; function fetchData(url_api, callback) { + let xhttp = new XMLHttpRequest(); xhttp.onreadystatechange = function (event) { - if (xhttp.readyState === '4') { + if (xhttp.readyState == 4) { if (xhttp.status == 200) callback(null, xhttp.responseText); - else return callback(url_api); + else return console.log(`sucedió un error`) } }; - xhttp.open('GET', url_api, false); + xhttp.open('GET', url_api, true); 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) { + fetchData('https://rickandmortyapi.com/api/character/1', function (error2, data2) { if (error2) return console.error(error1); console.log('Segundo Llamado...') - fetchData(data2.origin.url, function (error3, data3) { + fetchData('https://rickandmortyapi.com/api/location/1', function (error3, data3) { if (error3) return console.error(error3); - console.log('Tercero Llamado...') - console.log('Personajes:' + ' ' + data1.info.count); + console.log('Tercer Llamado...') + console.log('Personajes:' + ' ' +data1.info.count); console.log('Primer Personaje:' + ' ' + data2.name); console.log('Dimensión:' + ' ' + data3.dimension); }); From e5477e17429cc9fbf648369777848e82c2e6a611 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mar=C3=ADa=20P=C3=A9rez?= Date: Tue, 2 Jun 2020 12:14:19 -0500 Subject: [PATCH 2/4] First problem: resolved --- PULL_REQUEST_TEMPLATE.md | 8 ++++---- src/index.js | 8 ++++---- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/PULL_REQUEST_TEMPLATE.md b/PULL_REQUEST_TEMPLATE.md index e85525b..5b03e4b 100644 --- a/PULL_REQUEST_TEMPLATE.md +++ b/PULL_REQUEST_TEMPLATE.md @@ -1,13 +1,13 @@ ## DESCRIPTION -Nombre: -Usuario Platzi: +Nombre: María Angélica Pérez Gutiérrez +Usuario Platzi: Maria_PerezG ## Ciudad - [ ] Ciudad de México -- [ ] Bogotá +- [x] Bogotá # Retos: - - [ ] Primer problema + - [Done] Primer problema - [ ] Segundo problema - [ ] Tercer problema diff --git a/src/index.js b/src/index.js index 870a588..2801c08 100644 --- a/src/index.js +++ b/src/index.js @@ -1,4 +1,4 @@ -let XMLHttpRequest = require("xmlhttprequest").XMLHttpRequest; +const XMLHttpRequest = require("xmlhttprequest").XMLHttpRequest; const API = 'https://rickandmortyapi.com/api/character/'; @@ -7,7 +7,7 @@ function fetchData(url_api, callback) { xhttp.onreadystatechange = function (event) { if (xhttp.readyState == 4) { if (xhttp.status == 200) - callback(null, xhttp.responseText); + callback(null, JSON.parse(xhttp.responseText)); else return console.log(`sucedió un error`) } }; @@ -18,10 +18,10 @@ function fetchData(url_api, callback) { fetchData(API, function (error1, data1) { if (error1) return console.error('Error' + ' ' + error1); console.log('Primer Llamado...') - fetchData('https://rickandmortyapi.com/api/character/1', function (error2, data2) { + fetchData(API + data1.results[0].id, function (error2, data2) { if (error2) return console.error(error1); console.log('Segundo Llamado...') - fetchData('https://rickandmortyapi.com/api/location/1', function (error3, data3) { + fetchData(data2.origin.url, function (error3, data3) { if (error3) return console.error(error3); console.log('Tercer Llamado...') console.log('Personajes:' + ' ' +data1.info.count); From f0fa1e715b9cdc4b40913926ee724b0cfa9fbd40 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mar=C3=ADa=20P=C3=A9rez?= Date: Tue, 2 Jun 2020 13:10:58 -0500 Subject: [PATCH 3/4] Second Problem: resolved --- 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 5b03e4b..3207526 100644 --- a/PULL_REQUEST_TEMPLATE.md +++ b/PULL_REQUEST_TEMPLATE.md @@ -9,5 +9,5 @@ Usuario Platzi: Maria_PerezG # Retos: - [Done] Primer problema - - [ ] Segundo problema + - [Done] Segundo problema - [ ] Tercer problema diff --git a/src/index.js b/src/index.js index 2801c08..97e97f0 100644 --- a/src/index.js +++ b/src/index.js @@ -4,7 +4,7 @@ const API = 'https://rickandmortyapi.com/api/character/'; function fetchData(url_api, callback) { let xhttp = new XMLHttpRequest(); - xhttp.onreadystatechange = function (event) { + xhttp.onreadystatechange = event=> { if (xhttp.readyState == 4) { if (xhttp.status == 200) callback(null, JSON.parse(xhttp.responseText)); @@ -15,18 +15,18 @@ function fetchData(url_api, callback) { 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) { +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(error2); + console.log(`Segundo Llamado...`) + fetchData(data2.origin.url, (error3, data3)=> { if (error3) return console.error(error3); - console.log('Tercer Llamado...') - console.log('Personajes:' + ' ' +data1.info.count); - console.log('Primer Personaje:' + ' ' + data2.name); - console.log('Dimensión:' + ' ' + data3.dimension); + console.log(`Tercer 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 cb622c27bdb7db31b6cbcc9cd8f350ab414ffe0e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mar=C3=ADa=20P=C3=A9rez?= Date: Tue, 2 Jun 2020 15:48:49 -0500 Subject: [PATCH 4/4] Third Problem: resolved --- PULL_REQUEST_TEMPLATE.md | 2 +- src/index.js | 50 ++++++++++++++++++++++++---------------- 2 files changed, 31 insertions(+), 21 deletions(-) diff --git a/PULL_REQUEST_TEMPLATE.md b/PULL_REQUEST_TEMPLATE.md index 3207526..87ee04f 100644 --- a/PULL_REQUEST_TEMPLATE.md +++ b/PULL_REQUEST_TEMPLATE.md @@ -10,4 +10,4 @@ Usuario Platzi: Maria_PerezG # Retos: - [Done] Primer problema - [Done] Segundo problema - - [ ] Tercer problema + - [Done] Tercer problema diff --git a/src/index.js b/src/index.js index 97e97f0..58f2e3a 100644 --- a/src/index.js +++ b/src/index.js @@ -2,31 +2,41 @@ const XMLHttpRequest = require("xmlhttprequest").XMLHttpRequest; const API = 'https://rickandmortyapi.com/api/character/'; -function fetchData(url_api, callback) { +function fetchData(url_api) { let xhttp = new XMLHttpRequest(); + return new Promise ((resolve, reject)=>{ xhttp.onreadystatechange = event=> { if (xhttp.readyState == 4) { if (xhttp.status == 200) - callback(null, JSON.parse(xhttp.responseText)); - else return console.log(`sucedió un error`) - } + resolve(JSON.parse(xhttp.responseText)); + else return reject(url_api); + }; }; - xhttp.open('GET', url_api, true); + xhttp.open('GET', url_api,true); xhttp.send(); + }); }; -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(error2); - console.log(`Segundo Llamado...`) - fetchData(data2.origin.url, (error3, data3)=> { - if (error3) return console.error(error3); - console.log(`Tercer 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 +function onerror(url_api){ + console.log(`sucedió un error :(`); +} + +fetchData(API) + .then((data)=> { + data1=data; + console.log(`Primer Llamado...`); + return fetchData(`${API}${data.results[0].id}`); + }) + .then((data)=> { + data2=data; + console.log(`Segundo Llamado...`); + return fetchData(`${data.origin.url}`); + }) + .then((data)=> { + data3=data; + console.log(`Tercer Llamado...`); + console.log(`Personajes: ${data1.info.count}`); + console.log(`Primer Personaje:${data2.name}`); + console.log(`Dimensión: ${data3.dimension}`); + }) + .catch(onerror); \ No newline at end of file