A callback is a piece of code (i.e. the address or reference of a function or method or a lambda expression) that is passed as an argument to other code, which is expected to call back (execute) the argument at some convenient time. This tag should be used with questions about an API that uses call backs to notify the caller when an action is complete. Use the event-handling tag for questions involving subscribing to events such as in a GUI framework.
In computer programming, a callback is a piece of executable code that is passed as an argument to other code, which is expected to call back (execute) the argument at some convenient time. The invocation may be immediate as in a synchronous callback or it might happen at later time, as in an asynchronous callback.
Wikipedia article: http://en.wikipedia.org/wiki/Callback_(computer_programming)
An example of sync process callback in javascript:
function outer(argumentOne, fn){
console.log("what was argumentOne? ", argumentOne);
return fn();
}
function someCallback() {
console.log("callback triggered!");
}
outer("brown", someCallback);
An example of async process callback in javascript:
function mySandwich(param1, param2, callback) {
console.log('Started eating my sandwich.\n\n It has: ' + param1 + ', ' + param2);
setTimeout(function(){
callback(null,param1*param2);},2000);
}
mySandwich(1, 2, function(err,result) {
console.log('Finished eating my sandwich.' + result);
});
Output:
Started eating my sandwich.
It has: 1, 2
Finished eating my sandwich.2