From f2c3f25251811dc6b333bc341ef85f7579508654 Mon Sep 17 00:00:00 2001 From: jdavidzt Date: Thu, 24 Sep 2020 21:06:17 -0500 Subject: [PATCH] Se realizo retos en javascript-challenge --- package-lock.json | 13 +++++++++ package.json | 7 ++--- src/index.html | 11 ++++++++ src/index.js | 67 +++++++++++++++++++++++++++-------------------- 4 files changed, 66 insertions(+), 32 deletions(-) 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/package.json b/package.json index 65a8bf2..a5ac597 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "escuelajs-reto-03", - "version": "1.0.0", + "version": "0.1.0", "description": "Reto 3 Septiembre 14: Curso de Fundamentos de JavaScript", "main": "index.js", "scripts": { @@ -23,5 +23,6 @@ "homepage": "https://github.com/platzi/escuelajs-reto-03#readme", "dependencies": { "xmlhttprequest": "^1.8.0" - } -} \ No newline at end of file + }, + "devDependencies": {} +} diff --git a/src/index.html b/src/index.html new file mode 100644 index 0000000..cf79dd9 --- /dev/null +++ b/src/index.html @@ -0,0 +1,11 @@ + + + + + + Javascript Challenger + + + + + \ No newline at end of file diff --git a/src/index.js b/src/index.js index d6fa599..f5fbaea 100644 --- a/src/index.js +++ b/src/index.js @@ -1,32 +1,41 @@ -var XMLHttpRequest = require("xmlhttprequest").XMLHttpRequest; +const API = `https://rickandmortyapi.com/api/character/`; -var API = 'https://rickandmortyapi.com/api/character/'; -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); - else return callback(url_api); - } +const fetchData = (url_api) => { + return new Promise((resolve, reject) =>{ + const XMLHttpRequest = require("xmlhttprequest").XMLHttpRequest; + const xhttp = new XMLHttpRequest(); + xhttp.onreadystatechange = (event) => { + if (xhttp.readyState === 4) { + if (xhttp.status == 200) + resolve(xhttp.responseText); + else reject(Error(`${API}`)); + } + }; + xhttp.open('GET', url_api, true); + xhttp.send(); +}) }; - 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 + let dataOne + let dataTwo +fetchData(API) + .then (data1 =>{ + console.log(`Primer Llamado...`) + dataOne = JSON.parse(data1) + return fetchData(`${API} ${dataOne.results[0].id}`) + }) + .then(data2 =>{ + console.log(`Segundo Llamado...`) + dataTwo = JSON.parse(data2) + return fetchData(dataTwo.origin.url) + }) + .then(data3 =>{ + let dataThree = JSON.parse(data3) + console.log(`Tercero Llamado...`) + console.log(`Personajes: ${dataOne.info.count}`); + console.log(`Primer Personaje: ${dataTwo.name}`); + console.log(`Dimensión: ${dataThree.dimension}`); + }) + .catch((url_api)=>{ + console.log(`Error: ${url_api}`) + })