From 538af6fcb71df4b6190dc43231f434ce0c0047f9 Mon Sep 17 00:00:00 2001 From: Alejandro Cantillo Date: Thu, 13 Aug 2020 20:49:18 -0500 Subject: [PATCH] MOD the proyect problem solutions --- package-lock.json | 13 ++++++++++ package.json | 2 +- src/index.js | 65 +++++++++++++++++++++++++++-------------------- 3 files changed, 52 insertions(+), 28 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..440745c 100644 --- a/src/index.js +++ b/src/index.js @@ -1,32 +1,43 @@ -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) { - if (xhttp.readyState === '4') { - if (xhttp.status == 200) - callback(null, xhttp.responseText); - else return callback(url_api); - } - }; +const fetchData = url_api => { xhttp.open('GET', url_api, false); - xhttp.send(); + 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(); + }) }; -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 + +const getData = async url => { + try { + const first = await fetchData(url); + // console.log('Primer llamado'); + const second = await fetchData(`${API}${first.results[0].id}`); + // console.log('Segundo llamado'); + const third = await fetchData(second.origin.url); + // console.log('Tercer llamado'); + + 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);