Movatterモバイル変換


[0]ホーム

URL:


MDN Web Docs

Promise.any()

BaselineWidely available

ThePromise.any() static method takes an iterable of promises as input and returns a singlePromise. This returned promise fulfills when any of the input's promises fulfills, with this first fulfillment value. It rejects when all of the input's promises reject (including when an empty iterable is passed), with anAggregateError containing an array of rejection reasons.

Try it

const promise1 = Promise.reject(new Error("error"));const promise2 = new Promise((resolve) => setTimeout(resolve, 100, "quick"));const promise3 = new Promise((resolve) => setTimeout(resolve, 500, "slow"));const promises = [promise1, promise2, promise3];Promise.any(promises).then((value) => console.log(value));// Expected output: "quick"

Syntax

js
Promise.any(iterable)

Parameters

iterable

Aniterable (such as anArray) of promises.

Return value

APromise that is:

  • Already rejected, if theiterable passed is empty.
  • Asynchronously fulfilled, when any of the promises in the giveniterable fulfills. The fulfillment value is the fulfillment value of the first promise that was fulfilled.
  • Asynchronously rejected, when all of the promises in the giveniterable reject. The rejection reason is anAggregateError containing an array of rejection reasons in itserrors property. The errors are in the order of the promises passed, regardless of completion order. If theiterable passed is non-empty but contains no pending promises, the returned promise is still asynchronously (instead of synchronously) rejected.

Description

ThePromise.any() method is one of thepromise concurrency methods. This method is useful for returning the first promise that fulfills. It short-circuits after a promise fulfills, so it does not wait for the other promises to complete once it finds one.

UnlikePromise.all(), which returns anarray of fulfillment values, we only get one fulfillment value (assuming at least one promise fulfills). This can be beneficial if we need only one promise to fulfill but we do not care which one does. Note another difference: this method rejects upon receiving anempty iterable, since, truthfully, the iterable contains no items that fulfill. You may comparePromise.any() andPromise.all() withArray.prototype.some() andArray.prototype.every().

Also, unlikePromise.race(), which returns the firstsettled value (either fulfillment or rejection), this method returns the firstfulfilled value. This method ignores all rejected promises up until the first promise that fulfills.

Examples

Using Promise.any()

Promise.any() fulfills with the first promise to fulfill, even if a promise rejects first. This is in contrast toPromise.race(), which fulfills or rejects with the first promise to settle.

js
const pErr = new Promise((resolve, reject) => {  reject(new Error("Always fails"));});const pSlow = new Promise((resolve, reject) => {  setTimeout(resolve, 500, "Done eventually");});const pFast = new Promise((resolve, reject) => {  setTimeout(resolve, 100, "Done quick");});Promise.any([pErr, pSlow, pFast]).then((value) => {  console.log(value);  // pFast fulfills first});// Logs:// Done quick

Rejections with AggregateError

Promise.any() rejects with anAggregateError if no promise fulfills.

js
const failure = new Promise((resolve, reject) => {  reject(new Error("Always fails"));});Promise.any([failure]).catch((err) => {  console.log(err);});// AggregateError: No Promise in Promise.any was resolved

Displaying the first image loaded

In this example, we have a function that fetches an image and returns a blob. We usePromise.any() to fetch a couple of images and display the first one available (i.e., whose promise has resolved).

js
async function fetchAndDecode(url, description) {  const res = await fetch(url);  if (!res.ok) {    throw new Error(`HTTP error! status: ${res.status}`);  }  const data = await res.blob();  return [data, description];}const coffee = fetchAndDecode("coffee.jpg", "Coffee");const tea = fetchAndDecode("tea.jpg", "Tea");Promise.any([coffee, tea])  .then(([blob, description]) => {    const objectURL = URL.createObjectURL(blob);    const image = document.createElement("img");    image.src = objectURL;    image.alt = description;    document.body.appendChild(image);  })  .catch((e) => {    console.error(e);  });

Specifications

Specification
ECMAScript® 2026 Language Specification
# sec-promise.any

Browser compatibility

See also

Help improve MDN

Learn how to contribute.

This page was last modified on byMDN contributors.


[8]ページ先頭

©2009-2025 Movatter.jp