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.