Last active
October 10, 2017 23:06
-
-
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() {})`.
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
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