peterforgacs
7/26/2016 - 1:32 AM

Node callback

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);