Wait for the Fastest JavaScript Promise to Be Fulfilled with Promise.any()

InstructorMarius Schulz

Share this video with your friends

Send Tweet

The Promise.any() method accepts an array (or any other iterable) of promises as a parameter. It returns a Promise object that is fulfilled with the value of the first input promise to fulfill:

  • As soon as any input promise is fulfilled, the returned Promise object is fulfilled with that value.
  • If all input promises are rejected, the returned Promise object is rejected with an AggregateError which has an errors property containing an array of all rejection reasons.

Promise.any() can be used to race multiple promises against each other and find the first promise to be fulfilled.

Please note that at the moment, the Promise.any() method is only implemented in Firefox Nightly. Make sure to use a polyfill in your application to make it work across browsers.

Monica
~ 4 years ago

...Is 42 a Hitchhikers Guide to the Galaxy reference? πŸ˜‚

Marius Schulzinstructor
~ 4 years ago

...Is 42 a Hitchhikers Guide to the Galaxy reference? πŸ˜‚

Possibly! I can neither confirm nor deny. πŸ˜‰