Skip to content

Instantly share code, notes, and snippets.

@shigeki
Created July 4, 2012 11:21
Show Gist options
  • Save shigeki/3046839 to your computer and use it in GitHub Desktop.
Save shigeki/3046839 to your computer and use it in GitHub Desktop.
Domain Stack Sample
// domain stack sample
var domain = require('domain');
var d1 = domain.create();
var d2 = domain.create();
var d3 = domain.create();
d1.on('error', function(err) {
console.log('d1:', err.message);
});
d2.on('error', function(err) {
console.log('d2:', err.message);
});
d3.on('error', function(err) {
console.log('d3:', err.message);
});
function foo(arg) {
if (arg === 2) {
throw new Error('d2 error');
}
d3.run(function() {
throw new Error('d3 error');
});
}
function hoge(arg) {
if (arg === 1) {
throw new Error('d1 error');
}
d2.run(function() {
foo(arg);
});
}
d1.run(function() {
hoge(+process.argv[2]);
});
> node domain_stack.js 1
d1: d1 error
> node domain_stack.js 2
d2: d2 error
> node domain_stack.js 3
d3: d3 error
>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment