Promise.any() takes an iterable of Promise objects and, as soon as one of the promises in the iterable fulfils, returns a single promise that resolves with the value from that promise. If no promises in the iterable fulfil (if all of the given promises are rejected), then the returned promise is rejected with an AggregateError , a new subclass of Error that groups together individual errors. Essentially, this method is the opposite of Promise.all() .

Warning! Promise.any() method is experimental and not fully supported by all browsers and platforms. It is currently in stage 4 of the TC39 process .

句法

Promise.any(iterable);
						

参数

iterable
An iterable object, such as an Array .

返回值

  • An already resolved Promise iterable passed is empty.
  • An asynchronously resolved Promise iterable passed contains no promises.
  • A pending Promise in all other cases. This returned promise is then resolved/rejected asynchronously (as soon as the stack is empty) when any of the promises in the given iterable resolve, or if all the promises have rejected.

描述

This method is useful for returning the first promise that fulfils. It short-circuits after a promise fulfils, so it does not wait for the other promises to complete once it finds one. Unlike Promise.all() , which returns an array 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 fulfil but we do not care which one does. Also, unlike Promise.race() , which returns the first settled value (either fulfillment or rejection), this method returns the first fulfilled value. This method will ignore all rejected promises up until the first promise that fulfils.

Fulfilment

If any of the passed-in promises fulfil, the returned promise asynchronously fulfils with the value of the promise that fulfilled, whether or not the other promises have fulfilled or rejected.

  • If an empty iterable is passed, then this method returns (synchronously) an already resolved promise.
  • If any of the passed-in promises fulfill or are not promises, the promise returned by Promise.any is fulfilled asynchronously.

Rejection

If all of the passed-in promises reject, Promise.any asynchronously rejects with an AggregateError object, which extends Error , and contains an errors property with an array of rejection values.

范例

First to fulfil

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

const pErr = new Promise((resolve, reject) => {
  reject("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 fulfils first
})
// expected output: "Done quick"
							

Rejections with AggregateError

Promise.any() rejects with an AggregateError if no promise fulfils.

const pErr = new Promise((resolve, reject) => {
  reject('Always fails');
});
Promise.any([pErr]).catch((err) => {
  console.log(err);
})
// expected output: "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 use Promise.any() to fetch a couple of images and display the first one available (i.e. whose promise has resolved).

function fetchAndDecode(url) {
  return fetch(url).then(response => {
    if(!response.ok) {
      throw new Error(`HTTP error! status: ${response.status}`);
    } else {
      return response.blob();
    }
  })
}
let coffee = fetchAndDecode('coffee.jpg');
let tea = fetchAndDecode('tea.jpg');
Promise.any([coffee, tea]).then(value => {
  let objectURL = URL.createObjectURL(value);
  let image = document.createElement('img');
  image.src = objectURL;
  document.body.appendChild(image);
})
.catch(e => {
  console.log(e.message);
});
							

规范

规范
Promise.any

浏览器兼容性

The compatibility table on this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request. 更新 GitHub 上的兼容性数据
Desktop Mobile Server
Chrome Edge Firefox Internet Explorer Opera Safari Android webview Chrome for Android Firefox for Android Opera for Android Safari on iOS Samsung Internet Node.js
any Chrome 85 Edge No Firefox 79 IE No Opera No Safari 14 WebView Android 85 Chrome Android 85 Firefox Android No Opera Android No Safari iOS 14 Samsung Internet Android No nodejs No

图例

完整支持

完整支持

不支持

不支持

另请参阅

元数据

  • 最后修改: