Created
January 17, 2012 01:09
-
-
Save fernandezpablo85/1623948 to your computer and use it in GitHub Desktop.
Calculator with multiple result value callback
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 (err, data) { | |
if (err) { | |
console.error('something went wrong', err); | |
} else { | |
console.log('the sum is', data); | |
} | |
}); |
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) { | |
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