JavaScript Callbacks

I will call back later!”

A callback is a function passed as an argument to another function

This technique allows a function to call another function

A callback function can run after another function has finished.

// The add() function is 
// called with arguments a, b
// and callback, callback 
// will be executed just
// after ending of add() function
function add(a, b, callback) {
    console.log(`The sum of ${a} 
    and ${b} is ${a + b}`);
    callback();
}
 
// The disp() function is called just
// after the ending of add() function
function disp() {
    console.log(`This must be printed
     after addition`);
}
 
// Calling add() function
add(5, 6, disp)

Reference

https://developer.mozilla.org/en-US/docs/Glossary/Callback_function

https://www.geeksforgeeks.org/promise-vs-callback-in-javascript/

https://www.w3schools.com/js/js_callback.asp

FavoriteLoadingAdd to favorites
Spread the love

Author: Shahzad Khan

Software developer / Architect