Promise.any()
The Promise.any()
method takes an iterable of promises as input and returns a single Promise
. 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 an AggregateError
containing an array of rejection reasons.
Syntax
Promise.any(iterable)
Parameters
iterable
- : An iterable (such as an
Array
of promises.
- : An iterable (such as an
Return value
A Promise
that is:
- Already rejected, if the
iterable
passed is empty. - Asynchronously fulfilled, when any of the promises in the given
iterable
fulfills. The fulfillment value is the fulfillment value of the first promise that was fulfilled. - Asynchronously rejected, when all of the promises in the given
iterable
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
The Promise.any()
method is one of the promise 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.
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 fulfill but we do not care which one does. Note another difference: this method rejects upon receiving an empty iterable, since, truthfully, the iterable contains no items that fulfill. You may compare Promise.any()
and Promise.all()
with Array.prototype.some()
and Array.prototype.every()
.
Also, unlike Promise.race()
, which returns the first settled value (either fulfillment or rejection), this method returns the first fulfilled value. This method ignores all rejected promises up until the first promise that fulfills.