Promise.allSettled
JS Functions
Medium
15 min
promises
async
polyfills
Implement promiseAllSettled(iterable), which behaves like Promise.allSettled. It returns a promise that always fulfills once every input has settled. The fulfillment value is an array of outcome objects, in input order:
{ status: 'fulfilled', value }for items that fulfilled{ status: 'rejected', reason }for items that rejected
const results = await promiseAllSettled([
Promise.resolve(1),
Promise.reject(new Error('nope')),
'plain',
]);
// [
// { status: 'fulfilled', value: 1 },
// { status: 'rejected', reason: Error('nope') },
// { status: 'fulfilled', value: 'plain' },
// ]
await promiseAllSettled([]); // []
Requirements
- Do not use any of the native
Promisecombinators (all,allSettled,any,race).new Promise,Promise.resolveand.thenare fine. - Accept any iterable. Plain values count as fulfilled, and thenables are adopted.
- Keep input order regardless of completion order.
- The outcome objects contain only
statusplusvalueorreason. - An empty iterable fulfills with
[]. A non-iterable argument makes the returned promise reject with aTypeError.
Follow-up
- When would you choose
allSettledoverallin a real UI? - How would you show partial results while the slow items are still pending?
Spotted a mistake or a rights concern? Report an issue