Created
July 6, 2012 00:11
-
-
Save shigeki/3057252 to your computer and use it in GitHub Desktop.
Domain Stack Sample #5
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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() { | |
}); | |
}); | |
}); | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
> 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