Skip to content

Instantly share code, notes, and snippets.

@nakamura-to
Created April 20, 2012 08:28
Show Gist options
  • Save nakamura-to/2427125 to your computer and use it in GitHub Desktop.
Save nakamura-to/2427125 to your computer and use it in GitHub Desktop.
Node.js Domain sample
var fs = require('fs');
var domain = require('domain');
function countChars(filename, callback) {
var d = domain.create().on('error', function (err) {
callback(err);
});
fs.readFile(filename, 'utf-8', d.intercept(function (_, data) {
callback(null, data.length);
}));
}
function main(args) {
var d = domain.create().on('error', function (err) {
console.error(err);
});
countChars(args[0], d.intercept(function (_, length) {
console.log(length);
}));
}
main(['path1']);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment