Last active
November 22, 2015 22:16
-
-
Save commuterjoy/a27411b46a5c9d6b1579 to your computer and use it in GitHub Desktop.
safe Promise.all - a Promise.all() that can handle exceptions and rejections
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 transforms = [ | |
Promise.resolve(1), | |
Promise.resolve(2), | |
Promise.reject(3), | |
new Promise((resolve, reject) => { throw "Oops" }) // uncaught | |
Promise.resolve(5) | |
]; | |
Promise.all(transforms.map((p) => { | |
return p.catch((err) => { | |
console.log('error', err); | |
return 'x'; | |
}); | |
})).then(a => console.log(a)); // yields [1, 2, "x", "x", "x", 5] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment