Skip to content

Instantly share code, notes, and snippets.

@tkissing
Created December 9, 2016 21:43
Show Gist options
  • Save tkissing/ab5b2817cc1c3820f569223e5ad58b0b to your computer and use it in GitHub Desktop.
Save tkissing/ab5b2817cc1c3820f569223e5ad58b0b to your computer and use it in GitHub Desktop.
Using throw inside new Promise()
new Promise((resolve, reject) => {
resolve(someArr.map(e => {
if (e.foo) {
reject(Error('Found a foo!'));
// the .map will continue to loop
// even resolve will still be called although it won't have any more effect
}
return e.bar;
}));
});
new Promise(resolve => {
resolve(someArr.map(e => {
if (e.foo) {
throw Error('Found a foo!');
// .map is aborted, resolve is never actually called, the promise is rejected immediately
}
return e.bar;
}));
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment