Skip to content
tezvyn:

Promise.any(): Get the Fastest Successful Result

Source: developer.mozilla.orgHardHow cards are made

Promise.any(): Get the Fastest Successful Result

Promise.any() is a race where only finishers count. It returns the value of the first promise to succeed, ignoring any that fail. Use it to query redundant endpoints and take the first successful response.

Why it exists

Promise.any() was created to handle redundancy gracefully. When you have multiple ways to get the same result, you often don't care about the ones that fail or are slow; you just want the first successful outcome as quickly as possible. This method simplifies that logic.

The mental model

Think of Promise.any() as sending out three couriers to retrieve a package from three different warehouses. You don't care if one courier gets a flat tire (a rejected promise) or another is stuck in traffic (a slow promise). The moment the first courier arrives with the package (the first fulfilled promise), your job is done, and you can send the others home.

How it works

You provide an iterable (like an array) of promises. Promise.any() returns a single new promise. This new promise fulfills as soon as any of the input promises fulfills, adopting its fulfillment value. It short-circuits, meaning it stops waiting once it has a winner. If and only if ALL input promises reject, the promise returned by Promise.any() will reject with a special AggregateError, which contains an array of all the individual rejection reasons.

When to use it

Use Promise.any() for failover and redundancy. A classic case is fetching a critical asset from multiple CDNs or API mirrors. You request the asset from all sources simultaneously and use whichever responds successfully first, improving resilience and speed.

When not to use it

Do not use Promise.any() when you need the results from every promise; use Promise.all() for that. Also, do not use it if you need to react to the very first promise that settles, whether fulfilled or rejected. For that, use Promise.race(). If a fast failure is a meaningful signal in your application, Promise.race() is the correct tool.

One canonical example

Let's try to fetch an image from two servers, where one is offline. const promise1 = fetch('//server-A.com/image.png'); // Rejects const promise2 = fetch('//server-B.com/image.png'); // Fulfills

const promises = [promise1, promise2];
Promise.any(promises).then((response) => console.log('Got a response from a working server!'));

This code will successfully log the message, using the response from server-B and ignoring the failure from server-A.

Interview question

Which scenario best describes when to use Promise.any()?

  • a.To wait for all promises to settle (fulfill or reject) and get their individual outcomes.
  • b.To ensure all provided promises fulfill successfully before proceeding with their combined results.
  • c.To get the value from the very first promise that either fulfills or rejects.
  • d.To retrieve the result from the first promise that successfully completes, ignoring any failures.Correct
Why?

The correct answer (D) accurately describes Promise.any()'s purpose: to get the first successful result, even if other promises fail. Option C describes Promise.race(), which settles with the first promise regardless of its outcome.

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