Skip to content

Instantly share code, notes, and snippets.

@Sleavely
Created October 19, 2017 11:41
Show Gist options
  • Save Sleavely/49531ecb78b5b5094f7137036d7d9d65 to your computer and use it in GitHub Desktop.
Save Sleavely/49531ecb78b5b5094f7137036d7d9d65 to your computer and use it in GitHub Desktop.
Simple showcase of how faulty catch() might behave different than you expect.
Promise.resolve(true)
.then(() => {
console.log('First then() was called!');
throw new Error('error!!!');
return true;
})
.catch((err) => {
console.log('First catch() was called');
console.log('Error: ', err);
/* throwing or rejecting will cause us to skip to the next catch() */
return Promise.reject('fuck this shit');
throw err;
/**
* If, however, we return a non-rejection, or dont do anything at all,
* we'll simply end up in the next then()
* because javascript assumes we handled the error and can move on
*/
return false; // <- This would simply be fed into the next then()
})
.then((res) => {
console.log('after-catch then()');
console.log('res', res);
})
.catch((err) => {
console.log('Seconds catch() was called.');
console.log('Error: ', err);
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment