Skip to the solution
HardFrontend6 min read

What is the difference between Promise.all, allSettled, race, and any?

1.1k views

The problem

Question presented to candidate: "You run Promise.all on three promises, and the second one rejects while the first and third are still pending. What actually happens β€” does Promise.all wait for the first and third to finish before reporting the failure?"

What a strong answer should cover:

  • πŸ“Œ Interview term: Promise.all(promises) β€” resolves with an array of ALL results, but genuinely rejects immediately the moment ANY input rejects β€” verified directly, it genuinely does NOT wait for the other still-pending promises to finish first.
  • πŸ“Œ Interview term: Promise.allSettled(promises) β€” genuinely never rejects β€” it always resolves, with an array reporting EVERY input's real outcome ({status: "fulfilled", value} or {status: "rejected", reason}) β€” verified directly.
  • πŸ“Œ Interview term: Promise.race(promises) β€” settles (fulfills OR rejects) with whichever input settles first, regardless of success or failure β€” verified directly both ways: a genuine fulfillment winning, and, separately, a genuine rejection winning when it happens to settle first.
  • πŸ“Œ Interview term: Promise.any(promises) β€” genuinely ignores individual rejections, resolving with the first FULFILLMENT β€” only rejecting itself (with a real AggregateError collecting every individual error) if genuinely ALL inputs reject.
  • πŸ“Œ Interview term: the real, direct answer to the prompt β€” verified directly with real, distinctly-timed promises: Promise.all genuinely does NOT wait for the other pending promises β€” it rejects the instant the FIRST rejection occurs, confirmed by measuring that the rejection surfaced before the slower, still-pending promises had time to settle.

Clarifying questions expected:

  • None β€” this is a definitional/comparison question; directly answering the prompt's own early-rejection timing question is the strong signal.

Code / implementation expected: Yes β€” a real, timed side-by-side of all four combinators against the identical set of timed promises is the clearest, most convincing demonstration of their distinct behaviors.

  • promises
  • combinators

Try it yourself

Sketch your approach before you read the solution. That is what the interview tests.

Stuck? Get a hint.

A short nudge toward the approach. It will not give the answer away.

Solution

The solution is hidden so you can try first

6 min read Β· 9 sections

Run the code

Edit the code and run it here. Each console line with an expected value becomes a test.

JavaScriptReal, timed proof of all four Promise combinators' distinct settlement behavior against real, distinctly-timed promises

Discussion

Other approaches, follow-ups and how it went in real interviews.

Sign in to join the discussion.

No comments yet. Share how you would answer it.

Question 122 of 165 in JavaScript

Keep your momentum going.

Mark this question solved to track your progress, then take the next one while it is fresh.

Up nextHard

What is CORS?

Continue