Skip to content

Instantly share code, notes, and snippets.

@shigeki
Created July 6, 2012 00:11
Show Gist options
  • Save shigeki/3057252 to your computer and use it in GitHub Desktop.
Save shigeki/3057252 to your computer and use it in GitHub Desktop.
Domain Stack Sample #5
// domain stack sample
var fs = require('fs'), domain = require('domain');
// >> ClassA
function ClassA() {
}
ClassA.prototype.fnA = function(cb) {
setTimeout(cb, 300);
throw new Error('fnA');
};
// >> ClassB
function ClassB() {
}
ClassB.prototype.fnB = function(cb) {
var dB = domain.create();
dB.on('error', function(e) {
console.log('This is DOMAIN 2.' + e.message);
});
dB.run(function() {
setTimeout(cb, 500);
throw new Error('fnB');
dB.dispose();
});
};
var d = domain.create();
d.on('error', function(e) {
console.log('This is DOMAIN 1.' + e.message);
});
d.run(function() {
var a = new ClassA();
var b = new ClassB();
a.fnA = d.bind(a.fnA);
a.fnA(function() {
// domain 1
b.fnB(function() {
// to be domain 2 but I won't.
a.fnA(function() {
});
});
});
});
> node domain_stack5.js
This is DOMAIN 1.fnA
This is DOMAIN 2.fnB
This is DOMAIN 1.fnA
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment