From 5efcade3efaf2b2b61d822d1072c982d61fe24da Mon Sep 17 00:00:00 2001 From: eliana marcela Date: Tue, 9 Jun 2020 23:02:02 -0500 Subject: [PATCH 1/4] cambios finales --- package.json | 5 ++- src/index.js | 120 ++++++++++++++++++++++++++++++++++++++------------- 2 files changed, 93 insertions(+), 32 deletions(-) diff --git a/package.json b/package.json index 65a8bf2..49f8018 100644 --- a/package.json +++ b/package.json @@ -22,6 +22,7 @@ }, "homepage": "https://github.com/platzi/escuelajs-reto-03#readme", "dependencies": { - "xmlhttprequest": "^1.8.0" + "xmlhttprequest": "^1.8." + } -} \ No newline at end of file +} diff --git a/src/index.js b/src/index.js index d6fa599..2e27040 100644 --- a/src/index.js +++ b/src/index.js @@ -1,32 +1,92 @@ -var XMLHttpRequest = require("xmlhttprequest").XMLHttpRequest; +const 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, project)=>{ + + xhttp.open('GET', url_api) + xhttp.send(); + xhttp.onreadystatechange = function (event) { + let xhttp = new XMLHttpRequest(); + if (xhttp.readyState === 4) { + if (xhttp.status == 200) + + resolve(JSON.parse(xhttp.responseText)); + else return reject(' ha fallado') + } } - }; - 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 + + }) +} + +function error(url_api){ + console.log(` 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(error); + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + From fea351a9a0a4b88a2e11151dfd19db4def0eeb69 Mon Sep 17 00:00:00 2001 From: eliana marcela Date: Tue, 9 Jun 2020 23:02:36 -0500 Subject: [PATCH 2/4] commit html --- src/index.html | 12 ++++++++++++ 1 file changed, 12 insertions(+) create mode 100644 src/index.html diff --git a/src/index.html b/src/index.html new file mode 100644 index 0000000..db8af0f --- /dev/null +++ b/src/index.html @@ -0,0 +1,12 @@ + + + + + + + Document + + + + + \ No newline at end of file From 9dc287e32935602fae0bb41c356ca285e960a5a7 Mon Sep 17 00:00:00 2001 From: eliana marcela Date: Sat, 20 Jun 2020 14:41:15 -0500 Subject: [PATCH 3/4] modificado --- package.json | 3 +- src/index.js | 151 ++++++++++++++++++++++++++++++++++++++------------- 2 files changed, 113 insertions(+), 41 deletions(-) diff --git a/package.json b/package.json index 49f8018..dbaa2f1 100644 --- a/package.json +++ b/package.json @@ -22,7 +22,6 @@ }, "homepage": "https://github.com/platzi/escuelajs-reto-03#readme", "dependencies": { - "xmlhttprequest": "^1.8." - + "xmlhttprequest": "^1.8.0" } } diff --git a/src/index.js b/src/index.js index 2e27040..e3e7c4a 100644 --- a/src/index.js +++ b/src/index.js @@ -1,49 +1,122 @@ -const XMLHttpRequest = require("xmlhttprequest").XMLHttpRequest; -const API = 'https://rickandmortyapi.com/api/character/' -const fetchData = url_api => { - return new Promise((resolve, project)=>{ - - xhttp.open('GET', url_api) - xhttp.send(); - xhttp.onreadystatechange = function (event) { - let xhttp = new XMLHttpRequest(); - if (xhttp.readyState === 4) { - if (xhttp.status == 200) - - resolve(JSON.parse(xhttp.responseText)); - else return reject(' ha fallado') - } +const XMLHttpRequest = require("xmlhttprequest").XMLHttpRequest; + +const API = 'https://rickandmortyapi.com/api/character/'; + +const xhttp = new XMLHttpRequest(); + +fetchData=(url_api)=>{ + + return new Promise ((resolve, reject)=>{ + xhttp.open('GET', url_api, false); + xhttp.onreadystatechange = ()=> { + if (xhttp.readyState === 4) { + if (xhttp.status === 200) + res = JSON.parse(xhttp.responseText); + return resolve(res); + }else { + return reject(new error('error has ocurred')) } + }; + + + xhttp.send(); +}) + +}; - }) -} -function error(url_api){ - console.log(` error:()`) +const getData= async url=>{ + try { + const first= await fetchData(url) //es el primer llamado a nuestra api vamos a traer los personajes + const second = await fetchData(`${API}${first.results[0].id}`) + const third =await fetchData(second.origin.url) + + console.log(`personajes ${first.info.count}`); + console.log(`primer personaje ${second.name}`); + console.log(`Dimension: ${third.dimension}`); + } catch (error) { + console.log(error) + } } +getData(API) +// fetchData(API, (error1, data1) => { +// if (error1) return console.error(`error ${erro1}`); +// 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); +// }); +// }); +// }); + + + + + + -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(error); + + + + + + + + + + + + +// const XMLHttpRequest = require("xmlhttprequest").XMLHttpRequest; +// const API = 'https://rickandmortyapi.com/api/character/' + +// const fetchData = url_api => { +// xhttp.open('GET', url_api) +// return new Promise((resolve, project)=>{ + +// xhttp.onreadystatechange = function (event) { +// let xhttp = new XMLHttpRequest(); +// if (xhttp.readyState === 4) { +// if (xhttp.status === 200) + +// resolve(JSON.parse(xhttp.responseText)); +// else return reject(' ha fallado') +// } +// } +// xhttp.send(); + +// }) +// } + +// function error(url_api){ +// console.log(` 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(error); From 016a5d5c1d93a8945ceac6577db2d444804f8785 Mon Sep 17 00:00:00 2001 From: eliana marcela Date: Sat, 20 Jun 2020 14:42:24 -0500 Subject: [PATCH 4/4] change2 --- package-lock.json | 13 +++++++++++++ 1 file changed, 13 insertions(+) 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=" + } + } +}