-
-
Save AhmedKorim/2c543a145cb35c17df7f2c7e937b9df4 to your computer and use it in GitHub Desktop.
Wait until all promises have completed even when some reject, with Promise.all
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | |
}) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment