Skip to content
tezvyn:

Promise.race(): First Promise to Settle Wins

Source: developer.mozilla.orgHardHow cards are made

Promise.race(): First Promise to Settle Wins

Promise.race() returns a promise that mirrors the outcome of the first promise in a set to finish—the winner takes all, whether it resolves or rejects. Use it to set a timeout on a network request.

Why it exists

Sometimes you have multiple asynchronous tasks, but you only care about the result of the very first one to complete, regardless of its success or failure. Promise.race() was created to handle this specific concurrency pattern, common in scenarios involving redundancy or time limits.

The mental model

Think of Promise.race() as a horse race where you've bet on the entire field. The moment the first horse crosses the finish line, the race is declared over. You get the result of that one horse, and it doesn't matter how the others finish. The race can end in a win (the promise fulfills) or a disqualification (the promise rejects).

How it works

You pass an iterable (like an array) of promises to Promise.race(). It returns a single new promise. This new promise waits. As soon as any one of the input promises settles—either by fulfilling with a value or rejecting with an error—the race() promise immediately adopts that same state. All other promises in the iterable are left to run to completion, but their final outcomes are ignored.

When to use it

The canonical use case is implementing a timeout for an async operation. You can race a fetch() request against a setTimeout() wrapped in a promise that rejects after a certain duration. If the fetch() is too slow, the timeout promise will "win" the race, causing the entire operation to reject.

When not to use it

Do not use Promise.race() when you need the results of all promises, or when you need to wait for all promises to complete even if some fail. A single, fast rejection will cause the entire race() to reject. For those scenarios, use Promise.all() (which is fail-fast for all succeeding) or Promise.allSettled() (which waits for all to finish, regardless of outcome).

One canonical example

To implement a timeout, you can race a network request against a timer. Imagine a request that takes 500ms to resolve and a timer promise that rejects after 200ms. When you pass both to Promise.race(), the timer will reject first. The race() promise immediately rejects with the timer's error, 'Request timed out', and the eventual success of the network request is ignored.

Interview question

When using Promise.race() for a network request with a timeout, what is the outcome if the timeout promise rejects before the network request resolves?

  • a.The Promise.race() promise will eventually resolve with the network request's data.
  • b.The Promise.race() promise will resolve with an array of results from both promises.
  • c.The Promise.race() promise will wait for both to complete and then reject if either failed.
  • d.The Promise.race() promise will reject with the timeout's error.Correct
Why?

Promise.race() settles with the outcome of the very first promise to settle, regardless of whether it resolves or rejects. If the timeout promise rejects first, the race() promise immediately rejects with that error. The network request's eventual resolution is ignored because it was not the first to settle.

Just read this? Test yourself on what you have been reading.

Read the original → developer.mozilla.org

You just looked this up. Could you explain it out loud?

That is the part interviews actually test. Tezvyn takes questions like this one and gives you what the interviewer is really checking, the answer that lands, and the mistake that ends the conversation, in the four minutes before your next meeting.

The iPhone app is on the way

We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.

Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.

Get it on Google PlayiPhone app coming soon

We are hiring for this. Open roles that interview on javascript — each one lists the topics its interview covers.

See open roles