callbacks

Callback

A function passed to some other function, which we assume will be invoked at some point.

The function 'calls back' back invoking the function you give it when it is done doing its work.

function greet(callback){
    console.log('Hello!');
    var data={
        name:'John Doe'
    };
    callback(data);
}

greet(function(data){
    console.log(data.name+' The callback was invoked!');
    console.log(data.name);
});

greet(function(data){
    console.log(data + ' The callback was invoked!');
    console.log(data);
});

ERROR-FIRST CALLBACK

  • Callbacks take an error object as their first parameter

  • Null if no error, otherwise will contain an object defining the error.

  • This is a standard so we know in what order to place our parameters for our callbacks.

Last updated

Was this helpful?