From 55ea73cdb74181d12cafa8876ee6631dc8b6ca2b Mon Sep 17 00:00:00 2001 From: Alexander Guaman Cruz Date: Sun, 7 Jun 2020 20:33:36 -0500 Subject: [PATCH 1/2] Cloning and challenge finished --- PULL_REQUEST_TEMPLATE.md | 10 +++---- package.json | 3 +- src/index.html | 10 +++++++ src/index.js | 65 +++++++++++++++++++++------------------- 4 files changed, 52 insertions(+), 36 deletions(-) create mode 100644 src/index.html diff --git a/PULL_REQUEST_TEMPLATE.md b/PULL_REQUEST_TEMPLATE.md index e85525b..74b25ee 100644 --- a/PULL_REQUEST_TEMPLATE.md +++ b/PULL_REQUEST_TEMPLATE.md @@ -1,13 +1,13 @@ ## DESCRIPTION Nombre: -Usuario Platzi: +Usuario Platzi: Alexander Guamán Cruz ## Ciudad - [ ] Ciudad de México -- [ ] Bogotá +- [x] Bogotá # Retos: - - [ ] Primer problema - - [ ] Segundo problema - - [ ] Tercer problema + - [x] Primer problema + - [x] Segundo problema + - [x] Tercer problema diff --git a/package.json b/package.json index 65a8bf2..744f791 100644 --- a/package.json +++ b/package.json @@ -23,5 +23,6 @@ "homepage": "https://github.com/platzi/escuelajs-reto-03#readme", "dependencies": { "xmlhttprequest": "^1.8.0" + }, + "collaborator": "Alexander Guamán Cruz " } -} \ No newline at end of file diff --git a/src/index.html b/src/index.html new file mode 100644 index 0000000..bffbfb2 --- /dev/null +++ b/src/index.html @@ -0,0 +1,10 @@ + + + + + javascript-challenge + + + + + diff --git a/src/index.js b/src/index.js index d6fa599..501a270 100644 --- a/src/index.js +++ b/src/index.js @@ -1,32 +1,37 @@ -var XMLHttpRequest = require("xmlhttprequest").XMLHttpRequest; +const API = 'https://rickandmortyapi.com/api/character/' +const XMLHttpRequest = require("xmlhttprequest").XMLHttpRequest +const xhttp = new XMLHttpRequest() -var API = 'https://rickandmortyapi.com/api/character/'; -var xhttp = new XMLHttpRequest(); +function 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(`ha ocurrido un error inesperado`) + } + } + } + }) +} -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); - } - }; - 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 +Promise + .all([fetchData(API)]) + .then(data1 => { + console.log(`Primer Llamado...`) + console.log(`'Personajes: ${data1[0].info.count}`) + return fetchData(`${API}${data1[0].results[0].id}`) + }) + .then(data2 => { + console.log(`Segundo Llamado...`) + console.log(`Primer Personaje: ${data2.name}`) + return fetchData(data2.origin.url) + }) + .then(data3 => { + console.log(`Tercero Llamado...`) + console.log(`Dimensión: ${data3.dimension}`) + }) + .catch(message => console.log(message)) \ No newline at end of file From b5b2d3d7e34b062ec2063dd2d0697f833dd75c4b Mon Sep 17 00:00:00 2001 From: Alexander Guaman Cruz Date: Wed, 10 Jun 2020 22:17:52 -0500 Subject: [PATCH 2/2] challenge corretion --- .vscode/settings.json | 8 ++++++ package.json | 2 +- src/index.html | 2 +- src/index.js | 63 ++++++++++++++++++++++--------------------- 4 files changed, 43 insertions(+), 32 deletions(-) create mode 100644 .vscode/settings.json diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..1fe31be --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,8 @@ +{ + "cSpell.words": [ + "Personaje", + "Personajes", + "xhttp", + "xmlhttprequest" + ] +} \ No newline at end of file diff --git a/package.json b/package.json index 744f791..8b7eb11 100644 --- a/package.json +++ b/package.json @@ -4,7 +4,7 @@ "description": "Reto 3 Septiembre 14: Curso de Fundamentos de JavaScript", "main": "index.js", "scripts": { - "start": "node src/index.js" + "start": "src/index.js" }, "repository": { "type": "git", diff --git a/src/index.html b/src/index.html index bffbfb2..17c69c5 100644 --- a/src/index.html +++ b/src/index.html @@ -5,6 +5,6 @@ javascript-challenge - + diff --git a/src/index.js b/src/index.js index 501a270..ae47d0e 100644 --- a/src/index.js +++ b/src/index.js @@ -1,37 +1,40 @@ -const API = 'https://rickandmortyapi.com/api/character/' + const XMLHttpRequest = require("xmlhttprequest").XMLHttpRequest + +const API = 'https://rickandmortyapi.com/api/character/' const xhttp = new XMLHttpRequest() -function fetchData(url_api) { - return new Promise((resolve, reject) => { - xhttp.open('GET', url_api, true) - xhttp.send() - xhttp.onreadystatechange = event => { +const fetchData = url_api => { + xhttp.open('GET', url_api, false) + xhttp.responseType = 'json' + return new Promise((response, reject) => { + xhttp.onreadystatechange = () => { if (xhttp.readyState === 4) { - if (xhttp.status == 200) - resolve(JSON.parse(xhttp.responseText)) - else { - return reject(`ha ocurrido un error inesperado`) - } - } - } + if (xhttp.status === 200) { + const res = JSON.parse(xhttp.responseText) + return response(res) + } else { + return reject(new Error('Error has ocurred!')) + } + } + } + xhttp.send() }) } -Promise - .all([fetchData(API)]) - .then(data1 => { - console.log(`Primer Llamado...`) - console.log(`'Personajes: ${data1[0].info.count}`) - return fetchData(`${API}${data1[0].results[0].id}`) - }) - .then(data2 => { - console.log(`Segundo Llamado...`) - console.log(`Primer Personaje: ${data2.name}`) - return fetchData(data2.origin.url) - }) - .then(data3 => { - console.log(`Tercero Llamado...`) - console.log(`Dimensión: ${data3.dimension}`) - }) - .catch(message => console.log(message)) \ No newline at end of file +const getData = async url => { + try { + const first = await fetchData(url) + 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(`Dimensión: ${third.dimension}`) + + } catch (error) { + console.log(error) + } +} + +getData(API) \ No newline at end of file