DEV Community

Barrios Freddy
Barrios Freddy

Posted on

4 1

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.

SurveyJS custom survey software

JavaScript UI Libraries for Surveys and Forms

SurveyJS lets you build a JSON-based form management system that integrates with any backend, giving you full control over your data and no user limits. Includes support for custom question types, skip logic, integrated CCS editor, PDF export, real-time analytics & more.

Learn more

Top comments (0)

SurveyJS custom survey software

JavaScript Form Builder UI Component

Generate dynamic JSON-driven forms directly in your JavaScript app (Angular, React, Vue.js, jQuery) with a fully customizable drag-and-drop form builder. Easily integrate with any backend system and retain full ownership over your data, with no user or form submission limits.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay