From 1bc49fe92ed210535baad37c70a61ecfbc3b8fd5 Mon Sep 17 00:00:00 2001 From: nprimo Date: Thu, 9 Mar 2023 14:18:55 +0000 Subject: [PATCH] feat(kept-promise): improve subject --- subjects/devops/kept-promise/README.md | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/subjects/devops/kept-promise/README.md b/subjects/devops/kept-promise/README.md index 4950b897d..20121c578 100644 --- a/subjects/devops/kept-promise/README.md +++ b/subjects/devops/kept-promise/README.md @@ -2,22 +2,20 @@ ### Instructions -Thing in your code does not always happen as fast as you would like. +Things in your code do not always happen as fast as you would like. -Sometimes it is important to wait for a function to finish before keep going on. +Sometimes it is important to wait for a function to finish before keep going on. Other times you don't want your code to be blocking while waiting. Create an asynchronous function `processInfo` in a file `kept-promise.js`. The function `processInfo` will take an asynchronous function as an input, and it will print the message `Ok!` if the return of the input function is zero or a multiple of two, `Error!` otherwise. The following function can be used as an example of input function for `processInfo`: ```js -const getImportantInfo = _ => - new Promise(resolve => { - setTimeout(() => resolve(Math.round(Math.random() * 10)), 1000) - }) +const getImportantInfo = async _ => + new Promise(resolve => resolve(Math.round(Math.random() * 10))) ``` -> Assume that your function will always get a valid input function +> Assume that your function will always get a valid input function ### Example @@ -46,7 +44,13 @@ Ok! $ ``` +### Hints + +- It is possible to wait for an asynchronous function with the keyword `await`. Alternatively, it can be clearer to use the method `.then`. + ### Notions +- [Introducing asynchronous JavaScript](https://developer.mozilla.org/en-US/docs/Learn/JavaScript/Asynchronous/Introducing) +- [await](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/await) - [Using promises](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Using_promises) - [async function](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/async_function)