Browse Source

feat(kept-promise): add hints and suggestions

pull/1862/head
nprimo 2 years ago committed by Niccolò Primo
parent
commit
dcf7a3c6de
  1. 28
      subjects/devops/kept-promise/README.md

28
subjects/devops/kept-promise/README.md

@ -11,7 +11,7 @@ Create an asynchronous function `processInfo` in a file `kept-promise.js`. The f
The following function can be used as an example of input function for `processInfo`: The following function can be used as an example of input function for `processInfo`:
```js ```js
const getImportantInfo = async _ => const getImportantInfo = async () =>
new Promise(resolve => resolve(Math.round(Math.random() * 10))) new Promise(resolve => resolve(Math.round(Math.random() * 10)))
``` ```
@ -24,10 +24,8 @@ The following script `main.js` can be used to test your function:
```js ```js
import processInfo from './kept-promise.js' import processInfo from './kept-promise.js'
const getImportantInfo = _ => const getImportantInfo = () =>
new Promise(resolve => { new Promise(resolve => resolve(Math.round(Math.random() * 10)))
setTimeout(() => resolve(Math.round(Math.random() * 10)), 1000)
})
console.log(await processInfo(getImportantInfo)) console.log(await processInfo(getImportantInfo))
``` ```
@ -46,7 +44,24 @@ $
### Hints ### Hints
- It is possible to wait for an asynchronous function with the keyword `await`. Alternatively, it can be clearer to use the method `.then`. - A `Promise` is a special JS object that represent the result (succes or failure) of an asynchronous operation. This special object is usually used to "wrap" asynchronous operations.
`Promise`s can have three different states: _pending_ - the asynchronous operation has not finished yet, _fulfilled_/_resolved_ - the asynchronous operation has finished successfully - or _rejected_ - the asynchronous operation has finished, but something went wrong. When defined from scratch, it is possible to define a `resolve` and `reject` callback function for a new `Promise` that will define the results of the success or failure of asynchronous operation happening inside the `Promise`
- It is possible to wait for an asynchronous function with the keyword `await`. Alternatively, it can be clearer to use the method `.then`. Below an example of how to handle promises and, more generally, asynchronous operations.
```js
const promise1 = new Promise((resolve, reject) => {
resolve('Success!')
})
console.log(await promise1)
// Expected output: "Success!"
promise1.then(value => {
console.log(value)
// Expected output: "Success!"
})
```
### Notions ### Notions
@ -54,3 +69,4 @@ $
- [await](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/await) - [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) - [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) - [async function](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/async_function)
- [then](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/then)

Loading…
Cancel
Save