From 8b4f4ef96fcb363ff0e99204a5186ea693c1c205 Mon Sep 17 00:00:00 2001 From: Alfonso Neil Jimenez Casallas Date: Tue, 29 Sep 2020 16:46:22 -0500 Subject: [PATCH 1/4] =?UTF-8?q?Soluci=C3=B3n=20al=20primer=20problema?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...TEMPLATE.md\t" => PULL_REQUEST_TEMPLATE.md | 8 ++++---- src/index.js | 19 +++++++++++++++---- 2 files changed, 19 insertions(+), 8 deletions(-) rename "PULL_REQUEST_TEMPLATE.md\t" => PULL_REQUEST_TEMPLATE.md (62%) diff --git "a/PULL_REQUEST_TEMPLATE.md\t" b/PULL_REQUEST_TEMPLATE.md similarity index 62% rename from "PULL_REQUEST_TEMPLATE.md\t" rename to PULL_REQUEST_TEMPLATE.md index 9a4d861..17739e7 100644 --- "a/PULL_REQUEST_TEMPLATE.md\t" +++ b/PULL_REQUEST_TEMPLATE.md @@ -2,16 +2,16 @@ Solución al reto 04 de Escuela de JavaScript -Nombre: -Usuario Platzi: +Nombre: Alfonso Neil Jiménez Casallas +Usuario Platzi: @alfonsoneiljimenez ## GRUPO -- [ ] Martes +- [ X ] Martes - [ ] Miercoles - [ ] Jueves ## Reto: - - [ ] Primer problema + - [ X ] Primer problema - [ ] Segundo problema - [ ] Tercer problema - [ ] Cuarto Problema (Opcional) \ No newline at end of file diff --git a/src/index.js b/src/index.js index 4c965e7..31882fd 100644 --- a/src/index.js +++ b/src/index.js @@ -1,9 +1,20 @@ +const randomTime = (min, max) => { + min = Math.ceil(min); + max = Math.floor(max); + return Math.floor(Math.random() * (max - min + 1) + min); //The maximum is inclusive and the minimum is inclusive +} + const orders = (time, product, table) => { console.log(`### Orden: ${product} para ${table}`); return new Promise((resolve, reject) => { - setTimeout(() => { - resolve(`=== Pedido servido: ${product}, tiempo de preparación ${time}ms para la ${table}`); - }, time); + try{ + setTimeout(() => { + resolve(`=== Pedido servido: ${product}, tiempo de preparación ${time}ms para la ${table}`); + }, time); + }catch(err){ + reject(`Error during setup: ${err}`); + } + }); } @@ -16,7 +27,7 @@ const menu = { const table = ['Mesa 1', 'Mesa 2', 'Mesa 3', 'Mesa 4', 'Mesa 5']; const waiter = () => { - orders(6000, menu.hamburger, table[3]) + orders(randomTime(1000, 8000), menu.hamburger, table[3]) .then((res) => console.log(res)) .catch((err) => console.error(err)); }; From 95ed84f15c34d1dba46d8203636a79cb611fa17e Mon Sep 17 00:00:00 2001 From: Alfonso Neil Jimenez Casallas Date: Tue, 29 Sep 2020 18:31:06 -0500 Subject: [PATCH 2/4] =?UTF-8?q?Soluci=C3=B3n=20al=20segundo=20problema?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- PULL_REQUEST_TEMPLATE.md | 2 +- src/index.js | 12 +++++++++++- 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/PULL_REQUEST_TEMPLATE.md b/PULL_REQUEST_TEMPLATE.md index 17739e7..d12d75a 100644 --- a/PULL_REQUEST_TEMPLATE.md +++ b/PULL_REQUEST_TEMPLATE.md @@ -12,6 +12,6 @@ Usuario Platzi: @alfonsoneiljimenez ## Reto: - [ X ] Primer problema - - [ ] Segundo problema + - [ X ] Segundo problema - [ ] Tercer problema - [ ] Cuarto Problema (Opcional) \ No newline at end of file diff --git a/src/index.js b/src/index.js index 31882fd..b3ce881 100644 --- a/src/index.js +++ b/src/index.js @@ -12,7 +12,7 @@ const orders = (time, product, table) => { resolve(`=== Pedido servido: ${product}, tiempo de preparación ${time}ms para la ${table}`); }, time); }catch(err){ - reject(`Error during setup: ${err}`); + reject(`Error durante la entrega del pedido: ${err}`); } }); @@ -32,4 +32,14 @@ const waiter = () => { .catch((err) => console.error(err)); }; +const waiter2 = () => { + orders(randomTime(1000, 8000), menu.hotdog, table[0]) + .then((res) => console.log(res)); + return orders(randomTime(1000, 8000), menu.pizza, table[2]) + .then((res) => console.log(res)) + .catch((err) => console.error(err)); +}; + + waiter(); +waiter2(); \ No newline at end of file From 266ee05f7c8db64c1a5ae11bea5cff76453978f9 Mon Sep 17 00:00:00 2001 From: Alfonso Neil Jimenez Casallas Date: Tue, 29 Sep 2020 21:38:18 -0500 Subject: [PATCH 3/4] =?UTF-8?q?Soluci=C3=B3n=20al=20tercer=20problema?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- PULL_REQUEST_TEMPLATE.md | 2 +- src/index.js | 16 +++++++++++++++- 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/PULL_REQUEST_TEMPLATE.md b/PULL_REQUEST_TEMPLATE.md index d12d75a..2f45ea5 100644 --- a/PULL_REQUEST_TEMPLATE.md +++ b/PULL_REQUEST_TEMPLATE.md @@ -13,5 +13,5 @@ Usuario Platzi: @alfonsoneiljimenez ## Reto: - [ X ] Primer problema - [ X ] Segundo problema - - [ ] Tercer problema + - [ X ] Tercer problema - [ ] Cuarto Problema (Opcional) \ No newline at end of file diff --git a/src/index.js b/src/index.js index b3ce881..f6c6ca4 100644 --- a/src/index.js +++ b/src/index.js @@ -40,6 +40,20 @@ const waiter2 = () => { .catch((err) => console.error(err)); }; +const waiter3 = async() => { + try{ + const order1 = await orders(randomTime(1000, 8000), menu.hotdog, table[1]) + console.log(order1) + const order2 = await orders(randomTime(1000, 8000), menu.pizza, table[1]) + console.log(order2) + const order3 = await orders(randomTime(1000, 8000), menu.hotdog, table[1]) + console.log(order3) + }catch(err){ + console.error(err) + } +}; + waiter(); -waiter2(); \ No newline at end of file +waiter2(); +waiter3(); \ No newline at end of file From a227e3c6e66da5a103272b60132619cfb596a6c3 Mon Sep 17 00:00:00 2001 From: Alfonso Neil Jimenez Casallas Date: Tue, 29 Sep 2020 22:15:57 -0500 Subject: [PATCH 4/4] =?UTF-8?q?Soluci=C3=B3n=20al=20cuarto=20problema=20(o?= =?UTF-8?q?pcional)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- PULL_REQUEST_TEMPLATE.md | 2 +- package-lock.json | 13 ++++++++++++ package.json | 7 +++++-- src/index.js | 44 ++++++++++++++++++++++++++++++++++++++-- 4 files changed, 61 insertions(+), 5 deletions(-) create mode 100644 package-lock.json diff --git a/PULL_REQUEST_TEMPLATE.md b/PULL_REQUEST_TEMPLATE.md index 2f45ea5..cb62c29 100644 --- a/PULL_REQUEST_TEMPLATE.md +++ b/PULL_REQUEST_TEMPLATE.md @@ -14,4 +14,4 @@ Usuario Platzi: @alfonsoneiljimenez - [ X ] Primer problema - [ X ] Segundo problema - [ X ] Tercer problema - - [ ] Cuarto Problema (Opcional) \ No newline at end of file + - [ X ] Cuarto Problema (Opcional) \ No newline at end of file diff --git a/package-lock.json b/package-lock.json new file mode 100644 index 0000000..c90c067 --- /dev/null +++ b/package-lock.json @@ -0,0 +1,13 @@ +{ + "name": "escuelajs-reto-04", + "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 bf060d5..0abd8ad 100644 --- a/package.json +++ b/package.json @@ -20,5 +20,8 @@ "bugs": { "url": "https://github.com/platzi/escuelajs-reto-04/issues" }, - "homepage": "https://github.com/platzi/escuelajs-reto-04#readme" -} \ No newline at end of file + "homepage": "https://github.com/platzi/escuelajs-reto-04#readme", + "dependencies": { + "xmlhttprequest": "^1.8.0" + } +} diff --git a/src/index.js b/src/index.js index f6c6ca4..a189b3e 100644 --- a/src/index.js +++ b/src/index.js @@ -1,3 +1,27 @@ +const XMLHttpRequest = require("xmlhttprequest").XMLHttpRequest; + +const API = 'https://us-central1-escuelajs-api.cloudfunctions.net/orders'; +const xhttp = new XMLHttpRequest(); + + +const fetchOrders = (url_api) => { + xhttp.responseType = 'json'; + xhttp.open('GET', url_api, true); + xhttp.send(); + return new Promise((resolve, reject) => { + xhttp.onreadystatechange = (event) => { + if (xhttp.readyState === 4) { + if (xhttp.status == 200){ + const response = JSON.parse(xhttp.responseText); + resolve(response); + } + else reject(Error(`No fue posible entregar la petición desde ${url_api}`)); + } + } + }) +} + + const randomTime = (min, max) => { min = Math.ceil(min); max = Math.floor(max); @@ -12,7 +36,7 @@ const orders = (time, product, table) => { resolve(`=== Pedido servido: ${product}, tiempo de preparación ${time}ms para la ${table}`); }, time); }catch(err){ - reject(`Error durante la entrega del pedido: ${err}`); + reject(Error(`Error durante la entrega del pedido: ${err}`)); } }); @@ -53,7 +77,23 @@ const waiter3 = async() => { } }; +const waiter4 = async() => { + try{ + const order1 = await fetchOrders(API) + console.log(`=== Pedido servido: ${order1.data}`) + const order2 = await fetchOrders(API) + console.log(`=== Pedido servido: ${order2.data}`) + const order3 = await fetchOrders(API) + console.log(`=== Pedido servido: ${order3.data}`) + const order4 = await fetchOrders(API) + console.log(`=== Pedido servido: ${order4.data}`) + }catch(err){ + console.log(`Error durante la entrega del pedido: ${err}`) + } +}; + waiter(); waiter2(); -waiter3(); \ No newline at end of file +waiter3(); +waiter4(); \ No newline at end of file