Skip to the solution
EasyFrontend6 min read

Implement Promise.race()

886 views

The problem

Problem, as an interviewer would state it: "Implement Promise.race() yourself. Given an array of promises, return a new promise that settles — resolves OR rejects — the moment the FIRST input promise settles, matching whatever that promise did."

Examples:

Code
const fast = new Promise(r => setTimeout(() => r("fast"), 20));
const slow = new Promise(r => setTimeout(() => r("slow"), 100));
await Promise.race([slow, fast]); // "fast" - it settled first

Clarifying questions expected:

  • Does "first" mean first to RESOLVE specifically, or first to SETTLE at all (resolve OR reject)?
  • Can the input array contain non-promise values (plain values)?
  • What happens with an empty input array?

Code / implementation expected: Yes — real, direct proof that both a fast resolution and, separately, a fast rejection each correctly win a real race against a slower opposite outcome.

  • promise
  • promiseRace

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.race() correctly lets a fast rejection win over a slower resolution, matching native behavior — verified directly

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 26 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 nextEasy

Two Sum — Find indices of two numbers that add to target

Continue