From aab8b9a77276eb00e077914a3c4a32d415fb8a01 Mon Sep 17 00:00:00 2001 From: Angie Garcia Date: Mon, 28 Sep 2020 15:30:21 -0500 Subject: [PATCH 1/3] primer problema --- package-lock.json | 13 +++++++++++++ package.json | 2 +- src/index.js | 9 ++++++--- 3 files changed, 20 insertions(+), 4 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..748ed8f 100644 --- a/src/index.js +++ b/src/index.js @@ -5,9 +5,12 @@ 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){ + const objectchange = JSON.parse(xhttp.responseText) + callback(null, objectchange); + } + else return callback(url_api); } }; From f2052c2a37879143c4f202892f1acbf4e77fa2d2 Mon Sep 17 00:00:00 2001 From: Angie Garcia Date: Mon, 28 Sep 2020 17:21:29 -0500 Subject: [PATCH 2/3] segundo problema --- src/index.js | 29 +++++++++++++++-------------- 1 file changed, 15 insertions(+), 14 deletions(-) diff --git a/src/index.js b/src/index.js index 748ed8f..d44880c 100644 --- a/src/index.js +++ b/src/index.js @@ -1,10 +1,10 @@ -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) { +const fetchData = (url_api, callback) => { + xhttp.onreadystatechange = event => { if (xhttp.readyState === 4) { if (xhttp.status == 200){ const objectchange = JSON.parse(xhttp.responseText) @@ -18,18 +18,19 @@ 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) { + +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, 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(`Personajes: ${data1.info.count}`); + console.log(`Primer Personaje: ${data2.name}`); + console.log( `Dimensión: ${data3.dimension}`); }); }); }); \ No newline at end of file From 8e68131acecc372331b680ef9b86fe2bb0929ea0 Mon Sep 17 00:00:00 2001 From: Angie Garcia Date: Tue, 29 Sep 2020 16:59:08 -0500 Subject: [PATCH 3/3] reto promesas --- src/index.js | 47 +++++++++++++++++++++++++++-------------------- 1 file changed, 27 insertions(+), 20 deletions(-) diff --git a/src/index.js b/src/index.js index d44880c..cd759fd 100644 --- a/src/index.js +++ b/src/index.js @@ -3,34 +3,41 @@ const XMLHttpRequest = require("xmlhttprequest").XMLHttpRequest; const API = 'https://rickandmortyapi.com/api/character/'; const xhttp = new XMLHttpRequest(); -const fetchData = (url_api, callback) => { +const fetchdata = url_api => new Promise((resolve,reject)=>{ xhttp.onreadystatechange = event => { if (xhttp.readyState === 4) { - if (xhttp.status == 200){ + if (xhttp.status == 200) + { const objectchange = JSON.parse(xhttp.responseText) - callback(null, objectchange); + resolve(objectchange); } - else return callback(url_api); + else reject (url_api); } }; xhttp.open('GET', url_api, false); xhttp.send(); -}; +} +) +let data1 +let data2 + + +fetchdata(API) +.then((data)=>{console.log(`Primer Llamado...`) +data1=data +return fetchdata (API + data.results[0].id) +}) +.then((data)=> {console.log(`Segundo Llamado...`) +data2=data +return fetchdata (data.origin.url) +}) +.then ((data3)=> { + console.log('Tercero Llamado...') + console.log(`Personajes: ${data1.info.count}`); + console.log(`Primer Personaje: ${data2.name}`); + console.log( `Dimensión: ${data3.dimension}`); +}) +.catch((error)=> console.error(`Error ${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