Skip to content

Instantly share code, notes, and snippets.

@juliangruber
Created March 8, 2013 18:34
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save juliangruber/5118694 to your computer and use it in GitHub Desktop.
Save juliangruber/5118694 to your computer and use it in GitHub Desktop.
node.js domain example with event emitters
var domain = require('domain');
var EventEmitter = require('events').EventEmitter;
var inner = function (ee1, ee2, cb1, cb2) {
var domain1 = domain.createDomain();
domain1.on('error', function (err) {
domain1.dispose();
cb2();
});
domain1.add(ee1);
cb1();
};
var outter = function () {
var domain2 = domain.createDomain();
domain2.on('error', function (err) {
domain2.dispose();
console.log('success!');
});
var ee1 = new EventEmitter();
var ee2 = new EventEmitter();
domain2.add(ee2);
inner(ee1, ee2, function () {
ee1.emit('error', 'foo');
}, function () {
ee2.emit('error', 'bar');
});
};
outter();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment