Skip to content

Instantly share code, notes, and snippets.

@fernandezpablo85
Created January 17, 2012 01:09
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/1623948 to your computer and use it in GitHub Desktop.
Save fernandezpablo85/1623948 to your computer and use it in GitHub Desktop.
Calculator with multiple result value callback
var Calculator = require('./calc'); // considering we named the Calculator file 'calc.js' and it's on the same dir.
Calculator.add(1, 2, function (err, data) {
if (err) {
console.error('something went wrong', err);
} else {
console.log('the sum is', data);
}
});
var Calculator = {
// performs the complex computation of summing two integers :P
// returns 'error' and 'data'.
add : function (a, b, callback) {
try {
// we need the 'undefined' here since data is our second parameter.
callback(undefined, parseInt(a, 10) + parseInt(b, 10));
} catch (e) {
// we don't need the undefined param here but we'll add it for completion.
callback(e, undefined);
}
}
};
// 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