Skip to content
javascript
// 其区别于 Promise.race(), 尽管某个 promise 的 reject 早于另一个 promise 的 resolve,Promise.any() 仍将返回那个首先 resolve 的 promise。
Promise.any([
  Promise.reject('Third'),
  Promise.resolve('Second'),
  Promise.resolve('First'),
])
  .then((res) => console.log(res))
  .catch((err) => console.error(err))
// Second

Promise.any([
  Promise.reject('Error 1'),
  Promise.reject('Error 2'),
  Promise.reject('Error 3'),
])
  .then((res) => console.log(res))
  .catch((err) => console.error(err))
// AggregateError: All promises were rejected

Promise.any([
  Promise.resolve('Third'),
  Promise.resolve('Second'),
  Promise.resolve('First'),
])
  .then((res) => console.log(res))
  .catch((err) => console.error(err))
// Third