Last active
December 10, 2019 20:04
-
-
Save brettinternet/9dc3576c6041179a5a92ca544b2bf08f to your computer and use it in GitHub Desktop.
.Then Callback vs .Catch and how they're scoped
This file contains 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() | |
.then(() => { | |
throw new Error('This will be handled in the next .then(, err), OR in a .catch'); // comment this line to see the next error handled | |
}) | |
.then(() => { | |
throw new Error('This will only be handled in a .catch'); | |
}, error => { | |
// an alternative to .catch with less scope | |
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/then | |
// thus, with this method there's a block of code where errors could be left unhandled | |
console.error('onRejected function called: ' + error.message); | |
}) | |
.catch(error => { | |
// catch without onRejected will handle both error scenarios | |
console.error('Catch function called: ' + error.message) | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment