Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save ilabacheuski/93bb895247809d54a9b93d59441b3e77 to your computer and use it in GitHub Desktop.
Save ilabacheuski/93bb895247809d54a9b93d59441b3e77 to your computer and use it in GitHub Desktop.
Throws in Node JS Promises
// function promiseNotCatched() {
// return Promise.resolve().then(() => {throw new Error('panic!');});
// }
// function timeoutNotCatched() {
// setTimeout(() => {
// throw new Error('panic!')
// }, 0);
// }
// function timeoutIsSpecial() {
// setTimeout(() => {
// console.log('This will still run.');
// throw new Error('panic!')
// }, 500);
// // Intentionally cause an exception, but don't catch it.
// nonexistentFunc();
// console.log('This will not run.');
// }
// function throwInPromise() {
// return Promise.reject('panic!')
// }
// async function asyncSugar() {
// return await Promise.reject('panic!');
// }
// try {
// // promiseNotCatched(); // Not catched
// // promiseNotCatched().catch(e => console.log('catched')); // catched
// // timeoutNotCatched()
// // timeoutIsSpecial()
// throwInPromise()
// // asyncSugar()
// } catch(e) {
// console.log('catched');
// }
process.on('uncaughtException', (e, origin) => {
console.log('uncaughtException')
})
async function test2() {
return Promise.resolve().then(() => { throw new Error('Haha');});
}
async function test() {
try {
await test2();
} catch (error) {
console.log('catched');
}
}
test()
process.on('unhandledRejection', (reason, promise) => {
console.log('Unhandled Rejection at:', promise, 'reason:', reason);
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment