Implement Promise.all

MediumPromises & Async20 min5 tests

Implement your own version of Promise.all called promiseAll.

Requirements

  • Accept an array of promises (or values)
  • Return a promise that resolves with an array of results when all promises resolve
  • Reject immediately if any promise rejects
  • Maintain the order of results matching the input order

Example

promiseAll([Promise.resolve(1), Promise.resolve(2), 3])
  .then(console.log); // [1, 2, 3]
Hints (5)
  1. Return a new Promise that you control with resolve/reject.
  2. Use Promise.resolve() to wrap each item - this handles non-promise values.
  3. Track how many promises have resolved using a counter.
  4. Store results in an array at the correct index to maintain order.
  5. Call reject immediately when any promise fails - don't wait for others.

Topics

  • Promises & Async

Asked at

Meta · Google · Microsoft · Stripe

Join Us
blur