Skip to the page

Polyfills interviewers ask · core

Promise.all, allSettled, race and any polyfills

All four promise combinators share one skeleton — return a new Promise, turn the iterable into an array, wrap each item with Promise.resolve, store results by index with a counter — and differ only in when they resolve or reject.

The notes

What you must know

  • all: results[i] = value; if (--remaining === 0) resolve(results); reject on the first error; an empty input resolves [] immediately.
  • allSettled: map each to { status, value } or { status, reason }; never rejects.
  • race: for each item, Promise.resolve(item).then(resolve, reject); an empty input stays pending forever.
  • any: count rejections and store errors[i]; when all have rejected, reject(new AggregateError(errors, 'All promises were rejected')); an empty input rejects immediately.
  • Use a counter, not results.length (a sparse array has length before it is full), and never resolve when i === length - 1.
  • Accept any iterable (Array.from). Promise.resolve wraps plain values and thenables.
  • finally polyfill: then(v => Promise.resolve(fn()).then(() => v), e => Promise.resolve(fn()).then(() => { throw e; })).

The red pen

Common traps: mistake → fix

  • Mistake: Pushing results in completion order

    Fix: results must follow input order.

  • Mistake: Resolving when the last index finishes

    Fix: earlier indexes may still be pending.

  • Mistake: any rejecting on the first error

    Fix: it rejects only when all have rejected.

Easy to hard

Questions interviewers ask, easy to hard

  1. Write your own Promise.all. (Zepto 2025, Quizizz 2025)
  2. How do you handle non-promise values and an empty array?
  3. Now write allSettled.
  4. Implement Promise.any. What does it reject with? (Atlassian Karat)
  5. Implement Promise.prototype.finally.

Your first interview is free

Sign up and start with a 15-minute interview with the full report, and an English conversation. No card needed.

Start your free interview