Skip to content

Instantly share code, notes, and snippets.

@sairion
Created September 18, 2016 10:36
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 sairion/41e44878cc3d1d5269aa18e87d7061a8 to your computer and use it in GitHub Desktop.
Save sairion/41e44878cc3d1d5269aa18e87d7061a8 to your computer and use it in GitHub Desktop.
// Promise exception handling is actually handled by mulitple level - therfore you need to care about many things
new Promise((res, rej) => {
setTimeout(_ => {
// case 1) error outside of promise context
res('booom 2');
throw new Error('error 2'); // this won't get ignored like case 1 and have to be catched manually. Also this don't automatically goes to `onRejected` handler unlike case 1.
}, 3000);
// case 2)
// res('booom 1');
// throw new Error('error 1'); // This gets ignored because it is thrown after resolve function gets called.
})
.then(function onFulfilled(res) {
throw new Error('error in onFulfilled'); // This error will be catched and handled by to catch();, if fulfilled.
console.debug('fulfilled', res);
}, function onRejected(res) {
throw new Error('error in onRejected');
console.debug('rejected', res);
})
.catch(err => {
console.debug('Caught error from catch()', err)
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment