Skip to content

Instantly share code, notes, and snippets.

@fernandezpablo85
Created January 17, 2012 01:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fernandezpablo85/1623971 to your computer and use it in GitHub Desktop.
Save fernandezpablo85/1623971 to your computer and use it in GitHub Desktop.
var Calculator = require('./calc'); // considering we named the Calculator file 'calc.js' and it's on the same dir.
Calculator.add(1, 2, function (response) {
response.on('data', function (data) {
console.log('the sum is', data);
});
response.on('error', function (error) {
console.error('something went wrong', err);
});
});
var Calculator = {
// performs the complex computation of summing two integers :P
// returns 'error' and 'data'.
add : function (a, b, callback) {
// we define the handle for our client to attach events and we return it right away.
var event = new EventEmitter();
callback(event);
// now we perform the computation and emit the corresponding events.
try {
event.emit("data", parseInt(a,10) + parseInt(b,10));
} catch (e) {
event.emit("error", e);
}
}
};
// export the Calculator object
module.exports = Calculator;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment