This page is about callbacks in Javascript.
It is the primary method of enabling asynchrony.
callback = function () {
console.log("Hello Callback");
}
function highOrder(callbackFunctionToCall){
// Call the callback function
callbackFunctionToCall();
}
highOrder(callback);
With argument, you are not passing the function but the return value of the function. You need then to wrap it up in a anonymous function.
Example:
callback = function (who) {
return "Hello "+who;
}
function highOrder(callbackFunctionToCall){
// Call the callback function
console.log(callbackFunctionToCall());
}
highOrder(
function() {
return callback("Nico");
}
);
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Using_promises
doSomething(function(result) {
doSomethingElse(result, function(newResult) {
doThirdThing(newResult, function(finalResult) {
console.log('Got the final result: ' + finalResult);
}, failureCallback);
}, failureCallback);
}, failureCallback);
getData( a => {
getMoreData(a, b => {
getMoreData(b, c => {
getMoreData(c, d => {
getMoreData(d, e => {
console.log(e);
}
}
}
}
}