Create a gist now

Instantly share code, notes, and snippets.

Embed
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) {
console.log(err);
})
Promise.all(a.map(function(e) {
return fetch(e).catch(handleNetErr)
})).then(function(e) {
console.log('then',e) // Outputs: 'then', [Response, TypeError]
}).catch(function(e) {
console.log('err',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));
@mariojunior

This comment has been minimized.

Show comment
Hide comment
@mariojunior

mariojunior Apr 24, 2017

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

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

@yardensk

This comment has been minimized.

Show comment
Hide comment

nice

@andrei-cacio

This comment has been minimized.

Show comment
Hide comment
@andrei-cacio

andrei-cacio Oct 26, 2017

kudos! Was looking for a solution like this

kudos! Was looking for a solution like this

@titusjin

This comment has been minimized.

Show comment
Hide comment
@titusjin

titusjin Dec 23, 2017

awesome!! Thanks lots

awesome!! Thanks lots

@Argo-92

This comment has been minimized.

Show comment
Hide comment
@Argo-92

Argo-92 Feb 8, 2018

This was a great solution!

Argo-92 commented Feb 8, 2018

This was a great solution!

@scagood

This comment has been minimized.

Show comment
Hide comment
@scagood

scagood Mar 12, 2018

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

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

scagood commented Mar 12, 2018

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

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

@enapupe

This comment has been minimized.

Show comment
Hide comment
@enapupe

enapupe Jul 16, 2018

@scagood that's beautiful and very useful, ty

enapupe commented Jul 16, 2018

@scagood that's beautiful and very useful, ty

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