Search code examples
javascriptpromisees6-promise

What are the differences between Promise.all() and Promise.any() in JavaScript?


When working with the Promise object in JavaScript, I noticed two methods, which were Promise.all() and Promise.any().

Both of them take an iterable (such as an array) as a parameter. However, I can't find what each method does differently.

After searching it on MDN, it was still hard to find a summary for the differences between the two methods.

In conclusion, what are the differences between Promise.all() and Promise.any() in JavaScript?


Note: This question is not related to this question. Instead of Promise.race(), I am asking about Promise.all().


Solution

  • Promise.all only resolves when all Promises in the array passed in are resolved, and returns the array of resolved Promises. If any Promise inside the iterator rejects, Promise.all rejects (I assume with the reason of the original rejection).

    Promise.any resolves when any Promise in the array passed in, and returns the first Promise to do so. It doesn't reject unless all Promises in the iterator are rejected.