“In JavaScript, functions are first-class objects and can therefore be passed to other functions as parameters. This function that is passed is called a callback function. At some point in time in the future, or asynchronously, it will be “called back” by the function to which it is passed.”
Great. WTF that means?
If we execute the following code…
function myWorkout(exercise1, exercise2, callback) {
alert('Started my workout.\nToday my workout includes ' + exercise1+ ' and ' + exercise2);
callback();
}
myWorkout('paused squats', 'lunges', function() {
alert('Finished my workout!');
});
We will get
Started my workout.
Today my workout includes aquats and lunges
and followed by
Finished my workout!
Another Example:
function createScript(source, callback) {
let script = document.createElement('script');
script.src = source;
document.head.append(script);
//execute our callback
script.onload(function() {
callback(script);
});
}
createScript('/scripts/build.js', function() {
alertUser();
});
That means I’m calling a function and at the same time I’m sending a function as instruction (or parameter… whatever)
I can use any other word instead of callback anyway