From aebd6d07908020006935ab36ca93ec5e194849c2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E2=80=9CJhoancanchila=E2=80=9D?= Date: Sun, 21 Jun 2020 22:11:02 -0500 Subject: [PATCH 1/3] [ADD]functions javascript-challenge-02 --- .gitignore | 122 +++++++++++++++++------------------ LICENSE | 42 ++++++------ "PULL_REQUEST_TEMPLATE.md\t" | 17 ----- README.md | 104 ++++++++++++++--------------- package-lock.json | 13 ++++ package.json | 51 ++++++++------- src/index.js | 112 +++++++++++++++++++++++++------- 7 files changed, 262 insertions(+), 199 deletions(-) delete mode 100644 "PULL_REQUEST_TEMPLATE.md\t" create mode 100644 package-lock.json diff --git a/.gitignore b/.gitignore index ad46b30..bbd57d1 100644 --- a/.gitignore +++ b/.gitignore @@ -1,61 +1,61 @@ -# Logs -logs -*.log -npm-debug.log* -yarn-debug.log* -yarn-error.log* - -# Runtime data -pids -*.pid -*.seed -*.pid.lock - -# Directory for instrumented libs generated by jscoverage/JSCover -lib-cov - -# Coverage directory used by tools like istanbul -coverage - -# nyc test coverage -.nyc_output - -# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files) -.grunt - -# Bower dependency directory (https://bower.io/) -bower_components - -# node-waf configuration -.lock-wscript - -# Compiled binary addons (https://nodejs.org/api/addons.html) -build/Release - -# Dependency directories -node_modules/ -jspm_packages/ - -# TypeScript v1 declaration files -typings/ - -# Optional npm cache directory -.npm - -# Optional eslint cache -.eslintcache - -# Optional REPL history -.node_repl_history - -# Output of 'npm pack' -*.tgz - -# Yarn Integrity file -.yarn-integrity - -# dotenv environment variables file -.env - -# next.js build output -.next +# Logs +logs +*.log +npm-debug.log* +yarn-debug.log* +yarn-error.log* + +# Runtime data +pids +*.pid +*.seed +*.pid.lock + +# Directory for instrumented libs generated by jscoverage/JSCover +lib-cov + +# Coverage directory used by tools like istanbul +coverage + +# nyc test coverage +.nyc_output + +# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files) +.grunt + +# Bower dependency directory (https://bower.io/) +bower_components + +# node-waf configuration +.lock-wscript + +# Compiled binary addons (https://nodejs.org/api/addons.html) +build/Release + +# Dependency directories +node_modules/ +jspm_packages/ + +# TypeScript v1 declaration files +typings/ + +# Optional npm cache directory +.npm + +# Optional eslint cache +.eslintcache + +# Optional REPL history +.node_repl_history + +# Output of 'npm pack' +*.tgz + +# Yarn Integrity file +.yarn-integrity + +# dotenv environment variables file +.env + +# next.js build output +.next diff --git a/LICENSE b/LICENSE index 9905d73..0152f72 100644 --- a/LICENSE +++ b/LICENSE @@ -1,21 +1,21 @@ -MIT License - -Copyright (c) 2019 Comunidad Platzi - -Permission is hereby granted, free of charge, to any person obtaining a copy -of this software and associated documentation files (the "Software"), to deal -in the Software without restriction, including without limitation the rights -to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -copies of the Software, and to permit persons to whom the Software is -furnished to do so, subject to the following conditions: - -The above copyright notice and this permission notice shall be included in all -copies or substantial portions of the Software. - -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE -SOFTWARE. +MIT License + +Copyright (c) 2019 Comunidad Platzi + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git "a/PULL_REQUEST_TEMPLATE.md\t" "b/PULL_REQUEST_TEMPLATE.md\t" deleted file mode 100644 index 9a4d861..0000000 --- "a/PULL_REQUEST_TEMPLATE.md\t" +++ /dev/null @@ -1,17 +0,0 @@ -## DESCRIPTION - -Solución al reto 04 de Escuela de JavaScript - -Nombre: -Usuario Platzi: - -## GRUPO -- [ ] Martes -- [ ] Miercoles -- [ ] Jueves - -## Reto: - - [ ] Primer problema - - [ ] Segundo problema - - [ ] Tercer problema - - [ ] Cuarto Problema (Opcional) \ No newline at end of file diff --git a/README.md b/README.md index 35d54ca..72cbb10 100644 --- a/README.md +++ b/README.md @@ -1,52 +1,52 @@ -# javascript-challenge-02 -Reto 2 : Curso de Profesional de JavaScript - -# Ejecución - -``` -npm run start -``` - -### Primer problema -Crea una función llamada "randomTime" que te permita retornar un valor en mili segundos de forma aleatoria en el rango de 1000ms hasta 8000ms. - -* Completa la función "orders" manejando el reject. -* Utiliza la función de randomTime - -### Segundo Problema - -Crea una función llamada "waiter2" que se encargue de recoger dos pedidos, uno de la "Mesa 1" y otro de la "Mesa 3". - -Pedido "Mesa 1": Combo Hotdog -Pedido "Mesa 3": Combo Pizza - -* Utiliza Promesas Encadenadas -* Utiliza la función de randomTime - -### Tercer Problema - -Crea una función llamada "waiter3" que se encargue de recoger el pedido de la "Mesa 2" el pedido solo puede ser entregado hasta que todos los plantillos estén listos para ser servidos. - -* Pedido "Mesa 2": Combo Hotdog, Combo Pizza, Combo Hotdog - -* Utiliza Async/Await -* Manejo de errores -* Utiliza la función de randomTime - -### Cuarto Problema (Opcional) - -Crea una función llamada "fetchOrders" que realice un llamado a la API de ordenes y una función llamada "waiter4" que se encargue de solicitar 4 pedidos que deban de ser entregados hasta que estén todos listos. - -* API: https://us-central1-escuelajs-api.cloudfunctions.net/orders -* Utiliza Async/Await -* Manejo de errores - -# Enviar solución de reto - -Debes de crear un "Fork" de este proyecto, revolver los problemas y crear un Pull Request hacia este repositorio. - -# Contribuir -Si alguien quiere agregar o mejorar algo, lo invito a colaborar directamente en este repositorio: [javascript-challenge-02](https://github.com/platzi/javascript-challenge-02/) - -# Licencia -javascript-challenge-02 se lanza bajo la licencia [MIT](https://opensource.org/licenses/MIT). +# javascript-challenge-02 +Reto 2 : Curso de Profesional de JavaScript + +# Ejecución + +``` +npm run start +``` + +### Primer problema +Crea una función llamada "randomTime" que te permita retornar un valor en mili segundos de forma aleatoria en el rango de 1000ms hasta 8000ms. + +* Completa la función "orders" manejando el reject. +* Utiliza la función de randomTime + +### Segundo Problema + +Crea una función llamada "waiter2" que se encargue de recoger dos pedidos, uno de la "Mesa 1" y otro de la "Mesa 3". + +Pedido "Mesa 1": Combo Hotdog +Pedido "Mesa 3": Combo Pizza + +* Utiliza Promesas Encadenadas +* Utiliza la función de randomTime + +### Tercer Problema + +Crea una función llamada "waiter3" que se encargue de recoger el pedido de la "Mesa 2" el pedido solo puede ser entregado hasta que todos los plantillos estén listos para ser servidos. + +* Pedido "Mesa 2": Combo Hotdog, Combo Pizza, Combo Hotdog + +* Utiliza Async/Await +* Manejo de errores +* Utiliza la función de randomTime + +### Cuarto Problema (Opcional) + +Crea una función llamada "fetchOrders" que realice un llamado a la API de ordenes y una función llamada "waiter4" que se encargue de solicitar 4 pedidos que deban de ser entregados hasta que estén todos listos. + +* API: https://us-central1-escuelajs-api.cloudfunctions.net/orders +* Utiliza Async/Await +* Manejo de errores + +# Enviar solución de reto + +Debes de crear un "Fork" de este proyecto, revolver los problemas y crear un Pull Request hacia este repositorio. + +# Contribuir +Si alguien quiere agregar o mejorar algo, lo invito a colaborar directamente en este repositorio: [javascript-challenge-02](https://github.com/platzi/javascript-challenge-02/) + +# Licencia +javascript-challenge-02 se lanza bajo la licencia [MIT](https://opensource.org/licenses/MIT). diff --git a/package-lock.json b/package-lock.json new file mode 100644 index 0000000..10162c7 --- /dev/null +++ b/package-lock.json @@ -0,0 +1,13 @@ +{ + "name": "escuelajs-reto-04", + "version": "1.0.0", + "lockfileVersion": 1, + "requires": true, + "dependencies": { + "node-fetch": { + "version": "2.6.0", + "resolved": "https://registry.npmjs.org/node-fetch/-/node-fetch-2.6.0.tgz", + "integrity": "sha512-8dG4H5ujfvFiqDmVu9fQ5bOHUC15JMjMY/Zumv26oOvvVJjM67KF8koCWIabKQ1GJIa9r2mMZscBq/TbdOcmNA==" + } + } +} diff --git a/package.json b/package.json index bf060d5..39eab68 100644 --- a/package.json +++ b/package.json @@ -1,24 +1,27 @@ -{ - "name": "escuelajs-reto-04", - "version": "1.0.0", - "description": "Reto 4 Septiembre 21: Curso de Fundamentos de JavaScript", - "main": "index.js", - "scripts": { - "start": "node src/index.js" - }, - "repository": { - "type": "git", - "url": "git+https://github.com/platzi/escuelajs-reto-04.git" - }, - "keywords": [ - "javascript", - "escuelajs", - "node" - ], - "author": "Oscar Barajas ", - "license": "MIT", - "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 +{ + "name": "escuelajs-reto-04", + "version": "1.0.0", + "description": "Reto 4 Septiembre 21: Curso de Fundamentos de JavaScript", + "main": "index.js", + "scripts": { + "start": "node src/index.js" + }, + "repository": { + "type": "git", + "url": "git+https://github.com/platzi/escuelajs-reto-04.git" + }, + "keywords": [ + "javascript", + "escuelajs", + "node" + ], + "author": "Oscar Barajas ", + "license": "MIT", + "bugs": { + "url": "https://github.com/platzi/escuelajs-reto-04/issues" + }, + "homepage": "https://github.com/platzi/escuelajs-reto-04#readme", + "dependencies": { + "node-fetch": "^2.6.0" + } +} diff --git a/src/index.js b/src/index.js index 4c965e7..21dbb0e 100644 --- a/src/index.js +++ b/src/index.js @@ -1,24 +1,88 @@ -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); - }); -} - -const menu = { - hamburger: 'Combo Hamburguesa', - hotdog: 'Combo Hot Dogs', - pizza: 'Combo Pizza', -}; - -const table = ['Mesa 1', 'Mesa 2', 'Mesa 3', 'Mesa 4', 'Mesa 5']; - -const waiter = () => { - orders(6000, menu.hamburger, table[3]) - .then((res) => console.log(res)) - .catch((err) => console.error(err)); -}; - -waiter(); +const fetch = require("node-fetch") +const MAX = 8000; +const BASE_URL = "https://us-central1-escuelajs-api.cloudfunctions.net/orders" + +const table = ['Mesa 1', 'Mesa 2', 'Mesa 3', 'Mesa 4', 'Mesa 5']; +const randomTime = () => Math.floor(Math.random() * (MAX - 1000) + 1000) + + +const menu = { + hamburger: 'Combo Hamburguesa', + hotdog: 'Combo Hot Dogs', + pizza: 'Combo Pizza', +}; + +const orders = (time, product, table) => { + console.log(`### Orden: ${product} para ${table}`); + return new Promise((resolve, reject) => { + + if (time <= randomTime()) { + + setTimeout(() => { + resolve(`=== Pedido servido: ${product}, tiempo de preparación ${time}ms para la ${table}`); + + }, time); + } else { + + reject(`***Estamos presentando demoras en la preparación `) + } + + }); +} + +const waiter = () => { + orders(6000, menu.hamburger, table[3]) + .then(res => console.log(res)) + .catch(err => console.error(err)); +}; + +const waiter2 = () => { + orders(randomTime(), menu.hotdog, table[0]) + .then(res => console.log(res)) + .catch(err => console.error(err)); + + orders(randomTime(), menu.pizza, table[2]) + .then(res => console.log(res)) + .catch(err => console.error(err)); + +} + +const waiter3 = async () => { + + try { + + const arrayMenu = Object.values(menu) + const allOrders = arrayMenu.map(order => orders(randomTime(), order, table[1])) + + const allPromise = await Promise.all(allOrders) + console.log(allPromise.toString()) + } catch (error) { + + console.log(error) + + } + +} + +const fetchOrders = async () => { + + try { + const response = await fetch(BASE_URL) + const dataOrder = await response.json() + const nameOrder = await dataOrder.data + return console.log(nameOrder) + + } catch (err) { + console.error(`Error inesperado procesando la orden`, err) + } + +} + +waiter() +waiter2() +waiter3() +fetchOrders() + + + + From e266a5fcff460b1c8754aad67c07860c44b2e628 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E2=80=9CJhoancanchila=E2=80=9D?= Date: Sun, 21 Jun 2020 22:20:12 -0500 Subject: [PATCH 2/3] Revert "[ADD]functions javascript-challenge-02" This reverts commit aebd6d07908020006935ab36ca93ec5e194849c2. --- .gitignore | 122 +++++++++++++++++------------------ LICENSE | 42 ++++++------ "PULL_REQUEST_TEMPLATE.md\t" | 17 +++++ README.md | 104 ++++++++++++++--------------- package-lock.json | 13 ---- package.json | 51 +++++++-------- src/index.js | 112 +++++++------------------------- 7 files changed, 199 insertions(+), 262 deletions(-) create mode 100644 "PULL_REQUEST_TEMPLATE.md\t" delete mode 100644 package-lock.json diff --git a/.gitignore b/.gitignore index bbd57d1..ad46b30 100644 --- a/.gitignore +++ b/.gitignore @@ -1,61 +1,61 @@ -# Logs -logs -*.log -npm-debug.log* -yarn-debug.log* -yarn-error.log* - -# Runtime data -pids -*.pid -*.seed -*.pid.lock - -# Directory for instrumented libs generated by jscoverage/JSCover -lib-cov - -# Coverage directory used by tools like istanbul -coverage - -# nyc test coverage -.nyc_output - -# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files) -.grunt - -# Bower dependency directory (https://bower.io/) -bower_components - -# node-waf configuration -.lock-wscript - -# Compiled binary addons (https://nodejs.org/api/addons.html) -build/Release - -# Dependency directories -node_modules/ -jspm_packages/ - -# TypeScript v1 declaration files -typings/ - -# Optional npm cache directory -.npm - -# Optional eslint cache -.eslintcache - -# Optional REPL history -.node_repl_history - -# Output of 'npm pack' -*.tgz - -# Yarn Integrity file -.yarn-integrity - -# dotenv environment variables file -.env - -# next.js build output -.next +# Logs +logs +*.log +npm-debug.log* +yarn-debug.log* +yarn-error.log* + +# Runtime data +pids +*.pid +*.seed +*.pid.lock + +# Directory for instrumented libs generated by jscoverage/JSCover +lib-cov + +# Coverage directory used by tools like istanbul +coverage + +# nyc test coverage +.nyc_output + +# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files) +.grunt + +# Bower dependency directory (https://bower.io/) +bower_components + +# node-waf configuration +.lock-wscript + +# Compiled binary addons (https://nodejs.org/api/addons.html) +build/Release + +# Dependency directories +node_modules/ +jspm_packages/ + +# TypeScript v1 declaration files +typings/ + +# Optional npm cache directory +.npm + +# Optional eslint cache +.eslintcache + +# Optional REPL history +.node_repl_history + +# Output of 'npm pack' +*.tgz + +# Yarn Integrity file +.yarn-integrity + +# dotenv environment variables file +.env + +# next.js build output +.next diff --git a/LICENSE b/LICENSE index 0152f72..9905d73 100644 --- a/LICENSE +++ b/LICENSE @@ -1,21 +1,21 @@ -MIT License - -Copyright (c) 2019 Comunidad Platzi - -Permission is hereby granted, free of charge, to any person obtaining a copy -of this software and associated documentation files (the "Software"), to deal -in the Software without restriction, including without limitation the rights -to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -copies of the Software, and to permit persons to whom the Software is -furnished to do so, subject to the following conditions: - -The above copyright notice and this permission notice shall be included in all -copies or substantial portions of the Software. - -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE -SOFTWARE. +MIT License + +Copyright (c) 2019 Comunidad Platzi + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git "a/PULL_REQUEST_TEMPLATE.md\t" "b/PULL_REQUEST_TEMPLATE.md\t" new file mode 100644 index 0000000..9a4d861 --- /dev/null +++ "b/PULL_REQUEST_TEMPLATE.md\t" @@ -0,0 +1,17 @@ +## DESCRIPTION + +Solución al reto 04 de Escuela de JavaScript + +Nombre: +Usuario Platzi: + +## GRUPO +- [ ] Martes +- [ ] Miercoles +- [ ] Jueves + +## Reto: + - [ ] Primer problema + - [ ] Segundo problema + - [ ] Tercer problema + - [ ] Cuarto Problema (Opcional) \ No newline at end of file diff --git a/README.md b/README.md index 72cbb10..35d54ca 100644 --- a/README.md +++ b/README.md @@ -1,52 +1,52 @@ -# javascript-challenge-02 -Reto 2 : Curso de Profesional de JavaScript - -# Ejecución - -``` -npm run start -``` - -### Primer problema -Crea una función llamada "randomTime" que te permita retornar un valor en mili segundos de forma aleatoria en el rango de 1000ms hasta 8000ms. - -* Completa la función "orders" manejando el reject. -* Utiliza la función de randomTime - -### Segundo Problema - -Crea una función llamada "waiter2" que se encargue de recoger dos pedidos, uno de la "Mesa 1" y otro de la "Mesa 3". - -Pedido "Mesa 1": Combo Hotdog -Pedido "Mesa 3": Combo Pizza - -* Utiliza Promesas Encadenadas -* Utiliza la función de randomTime - -### Tercer Problema - -Crea una función llamada "waiter3" que se encargue de recoger el pedido de la "Mesa 2" el pedido solo puede ser entregado hasta que todos los plantillos estén listos para ser servidos. - -* Pedido "Mesa 2": Combo Hotdog, Combo Pizza, Combo Hotdog - -* Utiliza Async/Await -* Manejo de errores -* Utiliza la función de randomTime - -### Cuarto Problema (Opcional) - -Crea una función llamada "fetchOrders" que realice un llamado a la API de ordenes y una función llamada "waiter4" que se encargue de solicitar 4 pedidos que deban de ser entregados hasta que estén todos listos. - -* API: https://us-central1-escuelajs-api.cloudfunctions.net/orders -* Utiliza Async/Await -* Manejo de errores - -# Enviar solución de reto - -Debes de crear un "Fork" de este proyecto, revolver los problemas y crear un Pull Request hacia este repositorio. - -# Contribuir -Si alguien quiere agregar o mejorar algo, lo invito a colaborar directamente en este repositorio: [javascript-challenge-02](https://github.com/platzi/javascript-challenge-02/) - -# Licencia -javascript-challenge-02 se lanza bajo la licencia [MIT](https://opensource.org/licenses/MIT). +# javascript-challenge-02 +Reto 2 : Curso de Profesional de JavaScript + +# Ejecución + +``` +npm run start +``` + +### Primer problema +Crea una función llamada "randomTime" que te permita retornar un valor en mili segundos de forma aleatoria en el rango de 1000ms hasta 8000ms. + +* Completa la función "orders" manejando el reject. +* Utiliza la función de randomTime + +### Segundo Problema + +Crea una función llamada "waiter2" que se encargue de recoger dos pedidos, uno de la "Mesa 1" y otro de la "Mesa 3". + +Pedido "Mesa 1": Combo Hotdog +Pedido "Mesa 3": Combo Pizza + +* Utiliza Promesas Encadenadas +* Utiliza la función de randomTime + +### Tercer Problema + +Crea una función llamada "waiter3" que se encargue de recoger el pedido de la "Mesa 2" el pedido solo puede ser entregado hasta que todos los plantillos estén listos para ser servidos. + +* Pedido "Mesa 2": Combo Hotdog, Combo Pizza, Combo Hotdog + +* Utiliza Async/Await +* Manejo de errores +* Utiliza la función de randomTime + +### Cuarto Problema (Opcional) + +Crea una función llamada "fetchOrders" que realice un llamado a la API de ordenes y una función llamada "waiter4" que se encargue de solicitar 4 pedidos que deban de ser entregados hasta que estén todos listos. + +* API: https://us-central1-escuelajs-api.cloudfunctions.net/orders +* Utiliza Async/Await +* Manejo de errores + +# Enviar solución de reto + +Debes de crear un "Fork" de este proyecto, revolver los problemas y crear un Pull Request hacia este repositorio. + +# Contribuir +Si alguien quiere agregar o mejorar algo, lo invito a colaborar directamente en este repositorio: [javascript-challenge-02](https://github.com/platzi/javascript-challenge-02/) + +# Licencia +javascript-challenge-02 se lanza bajo la licencia [MIT](https://opensource.org/licenses/MIT). diff --git a/package-lock.json b/package-lock.json deleted file mode 100644 index 10162c7..0000000 --- a/package-lock.json +++ /dev/null @@ -1,13 +0,0 @@ -{ - "name": "escuelajs-reto-04", - "version": "1.0.0", - "lockfileVersion": 1, - "requires": true, - "dependencies": { - "node-fetch": { - "version": "2.6.0", - "resolved": "https://registry.npmjs.org/node-fetch/-/node-fetch-2.6.0.tgz", - "integrity": "sha512-8dG4H5ujfvFiqDmVu9fQ5bOHUC15JMjMY/Zumv26oOvvVJjM67KF8koCWIabKQ1GJIa9r2mMZscBq/TbdOcmNA==" - } - } -} diff --git a/package.json b/package.json index 39eab68..bf060d5 100644 --- a/package.json +++ b/package.json @@ -1,27 +1,24 @@ -{ - "name": "escuelajs-reto-04", - "version": "1.0.0", - "description": "Reto 4 Septiembre 21: Curso de Fundamentos de JavaScript", - "main": "index.js", - "scripts": { - "start": "node src/index.js" - }, - "repository": { - "type": "git", - "url": "git+https://github.com/platzi/escuelajs-reto-04.git" - }, - "keywords": [ - "javascript", - "escuelajs", - "node" - ], - "author": "Oscar Barajas ", - "license": "MIT", - "bugs": { - "url": "https://github.com/platzi/escuelajs-reto-04/issues" - }, - "homepage": "https://github.com/platzi/escuelajs-reto-04#readme", - "dependencies": { - "node-fetch": "^2.6.0" - } -} +{ + "name": "escuelajs-reto-04", + "version": "1.0.0", + "description": "Reto 4 Septiembre 21: Curso de Fundamentos de JavaScript", + "main": "index.js", + "scripts": { + "start": "node src/index.js" + }, + "repository": { + "type": "git", + "url": "git+https://github.com/platzi/escuelajs-reto-04.git" + }, + "keywords": [ + "javascript", + "escuelajs", + "node" + ], + "author": "Oscar Barajas ", + "license": "MIT", + "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 diff --git a/src/index.js b/src/index.js index 21dbb0e..4c965e7 100644 --- a/src/index.js +++ b/src/index.js @@ -1,88 +1,24 @@ -const fetch = require("node-fetch") -const MAX = 8000; -const BASE_URL = "https://us-central1-escuelajs-api.cloudfunctions.net/orders" - -const table = ['Mesa 1', 'Mesa 2', 'Mesa 3', 'Mesa 4', 'Mesa 5']; -const randomTime = () => Math.floor(Math.random() * (MAX - 1000) + 1000) - - -const menu = { - hamburger: 'Combo Hamburguesa', - hotdog: 'Combo Hot Dogs', - pizza: 'Combo Pizza', -}; - -const orders = (time, product, table) => { - console.log(`### Orden: ${product} para ${table}`); - return new Promise((resolve, reject) => { - - if (time <= randomTime()) { - - setTimeout(() => { - resolve(`=== Pedido servido: ${product}, tiempo de preparación ${time}ms para la ${table}`); - - }, time); - } else { - - reject(`***Estamos presentando demoras en la preparación `) - } - - }); -} - -const waiter = () => { - orders(6000, menu.hamburger, table[3]) - .then(res => console.log(res)) - .catch(err => console.error(err)); -}; - -const waiter2 = () => { - orders(randomTime(), menu.hotdog, table[0]) - .then(res => console.log(res)) - .catch(err => console.error(err)); - - orders(randomTime(), menu.pizza, table[2]) - .then(res => console.log(res)) - .catch(err => console.error(err)); - -} - -const waiter3 = async () => { - - try { - - const arrayMenu = Object.values(menu) - const allOrders = arrayMenu.map(order => orders(randomTime(), order, table[1])) - - const allPromise = await Promise.all(allOrders) - console.log(allPromise.toString()) - } catch (error) { - - console.log(error) - - } - -} - -const fetchOrders = async () => { - - try { - const response = await fetch(BASE_URL) - const dataOrder = await response.json() - const nameOrder = await dataOrder.data - return console.log(nameOrder) - - } catch (err) { - console.error(`Error inesperado procesando la orden`, err) - } - -} - -waiter() -waiter2() -waiter3() -fetchOrders() - - - - +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); + }); +} + +const menu = { + hamburger: 'Combo Hamburguesa', + hotdog: 'Combo Hot Dogs', + pizza: 'Combo Pizza', +}; + +const table = ['Mesa 1', 'Mesa 2', 'Mesa 3', 'Mesa 4', 'Mesa 5']; + +const waiter = () => { + orders(6000, menu.hamburger, table[3]) + .then((res) => console.log(res)) + .catch((err) => console.error(err)); +}; + +waiter(); From 09e40650316975b13775b324f591f4524c4fbc1f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E2=80=9CJhoancanchila=E2=80=9D?= Date: Sun, 21 Jun 2020 22:24:04 -0500 Subject: [PATCH 3/3] [ADD]functions javascript-challenge-02 --- src/index.js | 84 ++++++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 72 insertions(+), 12 deletions(-) diff --git a/src/index.js b/src/index.js index 4c965e7..b65c704 100644 --- a/src/index.js +++ b/src/index.js @@ -1,11 +1,9 @@ -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); - }); -} +const fetch = require("node-fetch") +const MAX = 8000; +const BASE_URL = "https://us-central1-escuelajs-api.cloudfunctions.net/orders" + +const table = ['Mesa 1', 'Mesa 2', 'Mesa 3', 'Mesa 4', 'Mesa 5']; +const randomTime = () => Math.floor(Math.random() * (MAX - 1000) + 1000) const menu = { hamburger: 'Combo Hamburguesa', @@ -13,12 +11,74 @@ const menu = { pizza: 'Combo Pizza', }; -const table = ['Mesa 1', 'Mesa 2', 'Mesa 3', 'Mesa 4', 'Mesa 5']; +const orders = (time, product, table) => { + console.log(`### Orden: ${product} para ${table}`); + return new Promise((resolve, reject) => { + + if (time <= randomTime()) { + + setTimeout(() => { + resolve(`=== Pedido servido: ${product}, tiempo de preparación ${time}ms para la ${table}`); + + }, time); + } else { + + reject(`***Estamos presentando demoras en la preparación `) + } + + }); +} const waiter = () => { orders(6000, menu.hamburger, table[3]) - .then((res) => console.log(res)) - .catch((err) => console.error(err)); + .then(res => console.log(res)) + .catch(err => console.error(err)); }; -waiter(); +const waiter2 = () => { + orders(randomTime(), menu.hotdog, table[0]) + .then(res => console.log(res)) + .catch(err => console.error(err)); + + orders(randomTime(), menu.pizza, table[2]) + .then(res => console.log(res)) + .catch(err => console.error(err)); + +} + +const waiter3 = async () => { + + try { + + const arrayMenu = Object.values(menu) + const allOrders = arrayMenu.map(order => orders(randomTime(), order, table[1])) + + const allPromise = await Promise.all(allOrders) + console.log(allPromise.toString()) + } catch (error) { + + console.log(error) + + } + +} + +const fetchOrders = async () => { + + try { + const response = await fetch(BASE_URL) + const dataOrder = await response.json() + const nameOrder = await dataOrder.data + return console.log(nameOrder) + + } catch (err) { + console.error(`Error inesperado procesando la orden`, err) + } + +} + +waiter() +waiter2() +waiter3() +fetchOrders() +