Implement Promise.allSettled
MediumPromises & Async20 min4 tests
Implement your own Promise.allSettled that waits for all promises to settle (resolve or reject) and returns an array of result objects.
Requirements
- Each result should be
{ status: 'fulfilled', value }or{ status: 'rejected', reason } - Never rejects — always resolves with the array of results
- Results should be in the same order as the input
Example
await promiseAllSettled([
Promise.resolve('a'),
Promise.reject('b'),
]); // [{ status: 'fulfilled', value: 'a' }, { status: 'rejected', reason: 'b' }]Hints (4)
- Return a Promise that always resolves.
- Track a counter for settled promises.
- On resolve, store
{ status: "fulfilled", value }. On reject, store{ status: "rejected", reason }. - When the counter reaches the total, resolve with the results array.
Topics
- Promises & Async
Asked at
Google · Amazon · Microsoft
