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)
  1. Return a Promise that always resolves.
  2. Track a counter for settled promises.
  3. On resolve, store { status: "fulfilled", value }. On reject, store { status: "rejected", reason }.
  4. When the counter reaches the total, resolve with the results array.

Topics

  • Promises & Async

Asked at

Google · Amazon · Microsoft

Join Us
blur