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()
.
Promise.all
only resolves when all Promise
s in the array passed in are resolved, and returns the array of resolved Promise
s. 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 Promise
s in the iterator are rejected.