Skip to content

Instantly share code, notes, and snippets.

@hsenag
Last active January 18, 2023 08:06
Show Gist options
  • Save hsenag/03d6d0a8e99e3894936512bba162b527 to your computer and use it in GitHub Desktop.
Save hsenag/03d6d0a8e99e3894936512bba162b527 to your computer and use it in GitHub Desktop.
// This code aborts (paste into a shell command-line, or drop the first and last lines and put in a `.js` or `.ts` file):
node << END
console.log("start");
async function throws() {
throw new Error("oops");
}
async function main() {
try {
const throwsPromise = throws();
await new Promise(setImmediate);
await throwsPromise;
} catch (e) {
console.log("caught");
}
console.log("finished");
}
main();
END
// But this doesn't:
node << END
console.log("start");
async function throws() {
throw new Error("oops");
}
async function main() {
try {
const throwsPromise = throws();
await throwsPromise;
} catch (e) {
console.log("caught");
}
console.log("finished");
}
main();
END
// And neither does this:
node --unhandled-rejections=none << END
console.log("start");
async function throws() {
throw new Error("oops");
}
async function main() {
try {
const throwsPromise = throws();
await new Promise(setImmediate);
await throwsPromise;
} catch (e) {
console.log("caught");
}
console.log("finished");
}
main();
END
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment