Skip to content

Instantly share code, notes, and snippets.

@owenallenaz
Last active March 5, 2022 15:30
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save owenallenaz/7141699 to your computer and use it in GitHub Desktop.
Save owenallenaz/7141699 to your computer and use it in GitHub Desktop.
Showing that nodeJS domains do not catch synchronous errors. Hence the common practice is to enclose them in process.nextTick()
var domain = require("domain");
var d = domain.create();
d.on("error", function() {
console.log("domain caught");
});
try {
d.run(function() {
process.nextTick(function() {
throw new Error("foo");
});
});
} catch (err) {
console.log("try/catch caught");
}
// result: domain caught
var domain = require("domain");
var d = domain.create();
d.on("error", function() {
console.log("domain caught");
});
try {
d.run(function() {
throw new Error("foo");
});
} catch (err) {
console.log("try/catch caught");
}
// result: try/catch caught
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment