Created
January 17, 2012 01:17
-
-
Save fernandezpablo85/1623971 to your computer and use it in GitHub Desktop.
This file contains 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
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); | |
}); | |
}); |
This file contains 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
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