Skip to content

Instantly share code, notes, and snippets.

@xeoncross
Last active October 10, 2017 23:06
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 xeoncross/3c235e71698941699f0b6429bfb309dd to your computer and use it in GitHub Desktop.
Save xeoncross/3c235e71698941699f0b6429bfb309dd to your computer and use it in GitHub Desktop.
Rejections and exceptions are the same thing to a promise. Don't try to catch() something if you have a second `.then(a, b)` function defined. Catch is just shorthand for `.then(undefined, function() {})`.
function promiseMe(exception) {
return new Promise((resolve, reject) => {
if (exception == 'exception') {
throw new Error('problem');
}
reject(new Error('reject-called'));
});
}
['exception', 'reject'].forEach((value) => {
promiseMe(value)
.then(() => {
console.log('never happen');
})
.catch((err) => {
console.log(1, value, 'catch', err.message);
});
promiseMe(value)
.then(() => {
console.log('never happen');
})
.then(undefined, (c) => {
console.log(2, value, 'then.c', c.message);
})
.catch((err) => {
console.log(2, value, 'catch', err.message);
});
promiseMe(value)
.then(
() => {
console.log('never happen');
},
(b) => {
console.log(3, value, 'then.b', b.message);
},
)
.then(undefined, (c) => {
console.log(3, value, 'then.c', c.message);
})
.catch((err) => {
console.log(3, value, 'catch', err.message);
});
});
/*
1 'exception' 'catch' 'problem'
2 'exception' 'then.c' 'problem'
3 'exception' 'then.b' 'problem'
1 'reject' 'catch' 'reject-called'
2 'reject' 'then.c' 'reject-called'
3 'reject' 'then.b' 'reject-called'
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment