Promise.any()和Promise.race()有什么区别

What is the difference between Promise.any() and Promise.race(), and how do they get used differently?

From MDN,

此外,与Promise.race()返回第一个结算值不同,   此方法返回第一个解析值。此方法将忽略   所有被拒绝的承诺直到第一个承诺解决为止。

So that brings me to, the difference between resolved and settled. Which then brought me to the MDN promises page, which then brought me to States and Fates

定居不是一个状态,只是一种语言上的便利。

So we have Promise.any and Promise.race for linguistic convenience? i.e. there is no difference. Another instance of this equality, is "A promise whose fate is unresolved is necessarily pending." and "We say that a promise is settled if it is not pending, i.e. if it is either fulfilled or rejected.".

因此,如果一个诺言得以解决,那么它就不会成为未解决的,因此它不会被处理。因此,如果它没有待处理,则解决。如此解决===解决了。

评论
  • reum
    reum 回复

    No, Promise.race and Promise.any do different things:

    Promise.race is settled as soon as any of the promises you feed it settle, whether they fulfill or reject.

    Promise.any is settled as soon as any of the promises you feed it fulfills or they all reject, in which case it's rejected with an AggregateError.

    主要区别在于:

    1. race rejects when the first promise you give it rejects; any doesn't, because another promise may fulfill instead

    2. any's rejection reason will be an AggregateError, but race's rejection reason will be whatever the rejection reason from the promise that rejected was.