Asynchronous code: callback functions

barriosdfreddy

Barrios Freddy

Posted on December 1, 2020

Asynchronous code: callback functions

Functions in JavaScript are not as in other languages are. In JavaScript, functions are first-class citizens. Therefore, a function can be passed as an argument, can be used as a variable value, and so on.

A callback function is a subroutine, It's a piece of code which can be executed immediately or later in run-time. As asynchronous callbacks, these functions are used to notify or alert when an event happens. Normally, these functions are specified as arguments to another one that start executing some code in the background, when the background code finishes running, it calls the callback function to let know the work is done or to tell you that something has happened.

function callback() {
    console.log("Process finished!");   
}

function run(callback) {
    for (let index = 0; index < 10000; index++) {
        console.log("Processing...");
    }
    callback();
}


run(callback);
Enter fullscreen mode Exit fullscreen mode

In simple words, an asynchronous callback is a function that can be invoked when you need to notify something happened or the work is done. Even the callbacks are old-fashioned, it's very important to know about them since a high number of APIs still use them.

💖 💪 🙅 🚩
barriosdfreddy
Barrios Freddy

Posted on December 1, 2020

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related

Asynchronous code: callback functions
javascript Asynchronous code: callback functions

December 1, 2020