Created
July 26, 2016 01:32
-
-
Save peterforgacs/210237e03783fa824238ddbefc3d036c to your computer and use it in GitHub Desktop.
Node callback
This file contains hidden or 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
// 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