-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
66 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
# Reto 21: Callbacks | ||
|
||
## Enunciado | ||
|
||
Explora el concepto de callback en tu lenguaje creando un ejemplo simple (a tu elección) que muestre su funcionamiento. | ||
|
||
DIFICULTAD EXTRA (opcional): | ||
|
||
Crea un simulador de pedidos de un restaurante utilizando callbacks. | ||
|
||
Estará formado por una función que procesa pedidos. | ||
|
||
Debe aceptar el nombre del plato, una callback de confirmación, una de listo y otra de entrega. | ||
|
||
- Debe imprimir un confirmación cuando empiece el procesamiento. | ||
- Debe simular un tiempo aleatorio entre 1 a 10 segundos entre procesos. | ||
- Debe invocar a cada callback siguiendo un orden de procesado. | ||
- Debe notificar que el plato está listo o ha sido entregado. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
/* eslint-disable no-console */ | ||
function processNumber(number, callback) { | ||
const result = number * number; | ||
callback(result); | ||
} | ||
|
||
function printResult(result) { | ||
// eslint-disable-next-line no-console | ||
console.log('El resultado es:', result); | ||
} | ||
|
||
// Usando la función con el callback | ||
processNumber(5, printResult); | ||
|
||
// DIFICULTAD | ||
function processOrder(dish, onConfirmation, onReady, onDelivered) { | ||
// Imprimir confirmación cuando empiece el procesamiento | ||
onConfirmation(dish); | ||
|
||
// Simular un tiempo aleatorio entre 1 a 10 segundos para cada etapa del proceso | ||
const randomDelay = () => Math.floor(Math.random() * 10000) + 1000; | ||
|
||
// Simular la confirmación del pedido | ||
setTimeout(() => { | ||
onReady(dish); // Notificar que el plato está listo | ||
|
||
// Simular la entrega del pedido | ||
setTimeout(() => { | ||
onDelivered(dish); // Notificar que el pedido ha sido entregado | ||
}, randomDelay()); | ||
}, randomDelay()); | ||
} | ||
|
||
// Callbacks para manejar las etapas del pedido | ||
function confirmOrder(dish) { | ||
console.log(`Pedido confirmado para: ${dish}`); | ||
} | ||
|
||
function dishReady(dish) { | ||
console.log(`El plato ${dish} está listo.`); | ||
} | ||
|
||
function orderDelivered(dish) { | ||
console.log(`El plato ${dish} ha sido entregado.`); | ||
} | ||
|
||
// Usando la función con los callbacks | ||
processOrder('Pizza Margherita', confirmOrder, dishReady, orderDelivered); |