Wait for all promises to complete even when some reject
Promise.all fails when one of the promises fail. Let's see how to prevent this
And the result is
Not much right? Even two from our three promises resolved successfully we see an error but we have no clue which one. It might be your use case that you want this behaviour from Promise.all but what if you want it to be resolved even if one of them fails. Let’s see how we can do that.
We can see the following result
What I’m doing here is basically appending a rejection handler callback to each promise. In this rejection handler I’m returning the Error object which is the reason why the promise failed. Returning from the catch internally calls then so this is why returning the Error object is like resolving with an error. So now values is an array and it will contain our Error object too. We now know that our second promise failed and we’re still able to see the resolved values of the other promises. Pretty cool I think.