Skip to the solution
MediumFrontend6 min read

Implement Promise.all() from scratch

1.0k views

The problem

Problem, as an interviewer would state it: "Implement Promise.all() yourself. Given an array of promises, return a single promise that resolves with an array of all their results — in the ORIGINAL input order, even if they finish in a different real order — and rejects immediately if any one of them rejects."

Examples:

Code
await myPromiseAll([slowPromise, fastPromise]);
// [slowResult, fastResult] - original order, even though fast finished first

Clarifying questions expected:

  • Does "fail fast" mean reject the moment the FIRST rejection happens, even while other promises are still pending?
  • Should a plain, non-promise value in the array be handled, or is every element guaranteed to already be a real promise?
  • What should happen with an empty input array?

Code / implementation expected: Yes — real, direct proof that results preserve original input order regardless of real completion order, and that a rejection genuinely short-circuits immediately.

  • promise
  • promiseAll
  • concurrency

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, direct proof: Promise.all() preserves original input order regardless of real completion order, and genuinely fails fast on rejection

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 89 of 154 in JavaScript coding

Keep your momentum going.

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

Up nextMedium

Implement throttle(func, wait, {leading, trailing})

Continue