implement Promise.allSettled()
2022年5月1日小于 1 分钟
Promise.allSettled()
implement Question
The Promise.allSettled() method returns a promise that resolves after all of the given promises have either fulfilled or rejected, with an array of objects that each describes the outcome of each promise.
- from MDN
Different from Promise.all()
which rejects right away once an error occurs, Promise.allSettled()
waits for all promises to settle.
Now can you implement your own allSettled()
?
note
Do not use Promise.allSettled() directly, it helps nothing.
Code
/**
* @param {Array<any>} promises - notice that input might contains non-promises
* @return {Promise<Array<{status: 'fulfilled', value: any} | {status: 'rejected', reason: any}>>}
*/
function allSettled(promises) {
// your code here
}
Related
- implement async helper -
parallel()
- implement
Promise.all()
- implement
Promise.any()
- implement
Promise.race()