Skip to content

Instantly share code, notes, and snippets.

@brettinternet
Last active December 10, 2019 20:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save brettinternet/9dc3576c6041179a5a92ca544b2bf08f to your computer and use it in GitHub Desktop.
Save brettinternet/9dc3576c6041179a5a92ca544b2bf08f to your computer and use it in GitHub Desktop.
.Then Callback vs .Catch and how they're scoped
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