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)
- Return a new Promise that you control with resolve/reject.
- Use
Promise.resolve()to wrap each item - this handles non-promise values. - Track how many promises have resolved using a counter.
- Store results in an array at the correct index to maintain order.
- Call reject immediately when any promise fails - don't wait for others.
Topics
- Promises & Async
Asked at
Meta · Google · Microsoft · Stripe
