Promise.all() — Handle all rejected promises

In this article, I use the Axios library as an example. It is a small AJAX library, that you can call like

axios('https://example.com/api')

This returns a promise that resolves when the AJAX is successful, and rejects when failes.

You might be familiar with Promise.all() in JavaScript. It is a neat way to take an array of Promises and combine them into a single promise — which resolves when all of the promises resolve, and rejects when atleast one of them failed.

All the individual promises will be executed parallely, but if any of the promise rejects, the overall promise will also get rejected, and there will be no way to know if the other promises got resolved or rejected.

Here is what you can do! Simply catch the individual promises, and store the errors manually

When we .catch() a Promise, the propogation of the reject will be stopped, therefore the combined Promise will never reject.

Note that, if any of the Promises get rejected, the resolved value in the .then() block, for the corresponding Promise will be undefined.

The above example was just a proof of concept, you can even do some fancy stuff like:

Hope you found this little article useful. Thanks for reading!

Coder, content creator. Admin of @baby_wolf_codes