Created
October 19, 2017 11:41
-
-
Save Sleavely/49531ecb78b5b5094f7137036d7d9d65 to your computer and use it in GitHub Desktop.
Simple showcase of how faulty catch() might behave different than you expect.
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
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