Skip to content

Instantly share code, notes, and snippets.

Last active November 15, 2022 18:11
What would you like to do?
Wait until all promises have completed even when some reject, with Promise.all
var a = ["sdfdf", "http://oooooolol"],
handleNetErr = function(e) { return e };
Promise.all(fetch('sdfdsf').catch(handleNetErr), fetch('http://invalidurl').catch(handleNetErr))
.then(function(sdf, invalid) {
console.log(sdf, invalid) // [Response, TypeError]
.catch(function(err) {
Promise.all( {
return fetch(e).catch(handleNetErr)
})).then(function(e) {
console.log('then',e) // Outputs: 'then', [Response, TypeError]
}).catch(function(e) {
let a = new Promise((res, rej) => res('Resolved!')),
b = new Promise((res, rej) => rej('Rejected!')),
c = a.catch(e => { console.log('"a" failed.'); return e; }),
d = b.catch(e => { console.log('"b" failed.'); return e; });
Promise.all([c, d])
.then((first, second) => console.log('Then', first, second)) // Then ["Resolved!", "Rejected!"]
.catch(err => console.log('Catch', err));
Promise.all([a.catch(e => e), b.catch(e => e)])
.then((first, second) => console.log('Then', first, second)) // Then ["Resolved!", "Rejected!"]
.catch(err => console.log('Catch', err));
Copy link

Thanks a lot. I've found your script when I was almost giving up and adding another lib to handle my promise. 👍

Copy link


Copy link

kudos! Was looking for a solution like this

Copy link

awesome!! Thanks lots

Copy link

This was a great solution!

Copy link

scagood commented Mar 12, 2018

Promise.almost = r => Promise.all( => p.catch ? p.catch(e => e) : p));

I added the 'almost' prototype to my promises, using this technique, nice solution.

Copy link

enapupe commented Jul 16, 2018

@scagood that's beautiful and very useful, ty

Copy link

Great. Thanks!

Copy link

Sweet! 😁

Copy link

Thanks. This should be inbuilt in ES6.

Copy link

Inspired from your examples, I created a small library

Copy link

Very nice. The one downside I see is that, since this forces all promises to resolve, we can't tell whether the output of the promise is an error string or an actual output. That is, not unless we return an object that has a field that tells us whether this output is a rejection or a normal output.

Copy link

abrilvg commented Nov 4, 2019

awesome!! Thanks lots

Copy link

Instead of using Promise.all() , you can use Promise.allSettled().

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment