Skip to content

Instantly share code, notes, and snippets.

@commuterjoy
Last active November 22, 2015 22:16
Show Gist options
  • Save commuterjoy/a27411b46a5c9d6b1579 to your computer and use it in GitHub Desktop.
Save commuterjoy/a27411b46a5c9d6b1579 to your computer and use it in GitHub Desktop.
safe Promise.all - a Promise.all() that can handle exceptions and rejections
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