From e9c7f84ac0986c23de93ce5490f745812461be4a Mon Sep 17 00:00:00 2001 From: WILMAR MALAVER Date: Tue, 9 Jun 2020 22:32:36 +0200 Subject: [PATCH 1/5] first problem solved --- package.json | 2 +- src/index.js | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) 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..4013121 100644 --- a/src/index.js +++ b/src/index.js @@ -5,9 +5,9 @@ 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); + if (xhttp.readyState === 4) { + if (xhttp.status === 200) + callback(null, JSON.parse (xhttp.responseText)); else return callback(url_api); } }; From f510b5d713c22b3337752d9cdb29cd20c8d72f5b Mon Sep 17 00:00:00 2001 From: WILMAR MALAVER Date: Tue, 9 Jun 2020 22:45:32 +0200 Subject: [PATCH 2/5] templates string --- src/index.js | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/src/index.js b/src/index.js index 4013121..c8fb711 100644 --- a/src/index.js +++ b/src/index.js @@ -1,7 +1,7 @@ -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(); function fetchData(url_api, callback) { xhttp.onreadystatechange = function (event) { @@ -17,16 +17,16 @@ function fetchData(url_api, callback) { fetchData(API, function (error1, data1) { if (error1) return console.error('Error' + ' ' + error1); - console.log('Primer Llamado...') + console.log(`Primer Llamado...`) fetchData(API + data1.results[0].id, function (error2, data2) { if (error2) return console.error(error1); - console.log('Segundo Llamado...') + 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 be491f1dba7991df0ee028e4600ac3c3d409012c Mon Sep 17 00:00:00 2001 From: WILMAR MALAVER Date: Tue, 9 Jun 2020 22:54:57 +0200 Subject: [PATCH 3/5] Arrow functions --- src/index.js | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/index.js b/src/index.js index c8fb711..02abe22 100644 --- a/src/index.js +++ b/src/index.js @@ -3,8 +3,8 @@ const XMLHttpRequest = require("xmlhttprequest").XMLHttpRequest; const API = 'https://rickandmortyapi.com/api/character/'; const 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) callback(null, JSON.parse (xhttp.responseText)); @@ -15,13 +15,13 @@ function fetchData(url_api, callback) { 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(error1); 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}`); From 8bc4b62f0f4a8527b777b261cbaef6baee9c3b50 Mon Sep 17 00:00:00 2001 From: WILMAR MALAVER Date: Wed, 10 Jun 2020 09:42:33 +0200 Subject: [PATCH 4/5] ECMAScript6(ES6) --- src/index.js | 57 ++++++++++++++++++++++++++++++---------------------- 1 file changed, 33 insertions(+), 24 deletions(-) diff --git a/src/index.js b/src/index.js index 02abe22..34b5fe1 100644 --- a/src/index.js +++ b/src/index.js @@ -3,30 +3,39 @@ const XMLHttpRequest = require("xmlhttprequest").XMLHttpRequest; const API = 'https://rickandmortyapi.com/api/character/'; const xhttp = new XMLHttpRequest(); -const fetchData = (url_api, callback) => { - xhttp.onreadystatechange = (event) => { - if (xhttp.readyState === 4) { - if (xhttp.status === 200) - callback(null, JSON.parse (xhttp.responseText)); - else return callback(url_api); +const 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, (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 + +Promise + .all([fetchData(API)]) + .then(data1 => { + console.log(`'Personajes: ${data1[0].info.count}`); + return fetchData(`${API}${data1[0].results[0].id}`) + +}) + +.then(data2 => { + console.log(`Primer Personaje: ${data2.name}`); + return fetchData(data2.origin.url) +}) + +.then(data3 => { + console.log(`Dimensión: ${data3.dimension}`); +}) + +.catch(message => console.log(message)) + \ No newline at end of file From db7923e664c7bde2a99c636818d90b30ca63088e Mon Sep 17 00:00:00 2001 From: WILMAR MALAVER Date: Wed, 10 Jun 2020 09:46:47 +0200 Subject: [PATCH 5/5] Template.md --- 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..1189ed9 100644 --- a/PULL_REQUEST_TEMPLATE.md +++ b/PULL_REQUEST_TEMPLATE.md @@ -1,13 +1,13 @@ ## DESCRIPTION -Nombre: -Usuario Platzi: +Nombre: wilmar yoan malaver +Usuario Platzi: @wilmaryoanmalavermalaver ## Ciudad - [ ] Ciudad de México -- [ ] Bogotá +- [done] Bogotá # Retos: - - [ ] Primer problema - - [ ] Segundo problema - - [ ] Tercer problema + - [done] Primer problema + - [done] Segundo problema + - [done] Tercer problema