Skip to content

Instantly share code, notes, and snippets.

@peterforgacs
Created July 26, 2016 01:32
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 peterforgacs/210237e03783fa824238ddbefc3d036c to your computer and use it in GitHub Desktop.
Save peterforgacs/210237e03783fa824238ddbefc3d036c to your computer and use it in GitHub Desktop.
Node callback
// Asynchronous
function myFunction(x, y, callback) {
if ( calback && typeof(callback) !== 'function' ) {
callback( new Error('First argument is not a number') );
return;
}
if ( x && typeof(x) !== 'number' ) {
callback( new Error('First argument is not a number') );
return;
}
if ( y && typeof(y) !== 'number' ) {
callback( new Error('Second argument is not a number') );
return;
}
var result = x + y;
setTimeout(function () {
callback(null, result);
}, 500);
}
function callback(err, data) {
if (err) {
console.log(err);
return;
}
console.log(data);
}
myFunction(2, 5, callback);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment