Skip to main content

Search

Search problems, hooks, and pages

Implement Promise.all

Write a polyfill for Promise.all(promises) that resolves with an array of results — in input order — once every promise resolves, and rejects as soon as any one of them rejects (fail-fast). This tests your understanding of promises, closures, and asynchronous control flow.

Examples

Input: promiseAll([Promise.resolve(1), 2, Promise.resolve(3)])
Output: [1, 2, 3]
// Non-promise values are treated as already-resolved; order matches the input.
Input: promiseAll([Promise.resolve(1), Promise.reject("boom")])
Output: Rejects with "boom"
// The first rejection rejects the whole aggregate immediately (fail-fast).

Constraints

  • Results must be returned in the original input order, not completion order.
  • Reject as soon as the first input promise rejects.
  • Handle an empty input by resolving with an empty array.
  • Treat non-promise values as already resolved.
promiseasyncpolyfillclosure

Important

Interview Tip

Track a completed counter and write results by index to preserve order — do not push. Wrap each item in Promise.resolve so plain values work. Be ready to contrast with allSettled (never rejects), race (first to settle), and any (first to fulfil).

Approach: Core Polyfill

Counter + indexed results array inside a new Promise. Resolve when all complete; reject on the first failure. The standard answer.

Complexity

Time: O(n)Space: O(n)

Pros

  • Preserves input order via index
  • Fail-fast on first rejection
  • Handles non-promise values

Cons

  • Array-only (no generic iterable)
  • No concurrency limit