Functions - JavaScript Callback Function
Posted by Superadmin on May 06 2023 05:14:40

JavaScript Callback Function

By Anusua DuttaAnusua Dutta
  

JavaScript Callback Function

Introduction to JavaScript Callback Function

JavaScript Callback function are the most special and important function of JavaScript whose main aim is to pass another function as a parameter where the callback function runs which means one function when infused into another function with the parameters is again called as per the requirement. The parameters passed by the function at the time of invocation must be kept in mind by passing otherwise it might create compilation error at the time of calling the function. Callback function in JavaScript can be called in two ways namely synchronously or asynchronously.

Syntax:

function func_One(p)
{
return p;
};
function func_two(vr1)
{
// Program Body
}
function func_two(func_One);

The syntax flow is as follows where the function func_One is the first function which passes the parameter p to the function and then returns the value for the first function. Simultaneously a second function is called where the second function passes the parameter as vr1 which gets infused with the second function as per the requirement and then it goes for the program body and after coming out of it calls for the second function which in this case is func_two(func_One) which signifies that it is using the callback function in JavaScript.

How Callback Function Works in JavaScript?

Types of JavaScript Callback Function

Below are the types of JavaScript Callback Function:

  1. Synchronous Callback Function
  2. Asynchronous Callback Function

1. Synchronous Callback Function

As its name suggests Callback function are said to synchronous callback function if the code in execution gets executed in a sequence without any halt or hindrance and gives the desired output smoothly formulated asynchronous callback function.

Below are the examples:

This program demonstrates that the number provided in the given array needs to be arranged in ascending without any halt which signifies successful execution of Synchronous Callback function.

Code:

let sm_num = [12,15,10,5,9,1,6];
sm_num.sort((l, n) => l - n);
console.log(sm_num);

Output:

JavaScript Callback Function Example 1

2. Asynchronous Callback Function

Asynchronous Callback Function is a kind of function where the JavaScript which contains the program logic needs to wait for completing the rest of the code in execution prior to which it will execute the next set of code while waiting. Also, JavaScript involves single threading which means that the threads responsible for performing the operations involve callback queuing and loop back for events. Therefore, when the callback function is still in execution after the wait are called as asynchronous callback function religiously.

Below are the examples:

This program is used to demonstrate the asynchronous callback function where the callback function calls the function after performing a wait of a certain time period once queued and is shown in the output.

function down_load(link, callback) {
setTimeout(() => {
console.log(`Download the link ${link} ...`);
callback(link);
}, 2000);
}
let link = 'https://educba.com';
down_load(link, function(data) {
console.log(`Check the Data ${data}`);
});

Output:

JavaScript Callback Function Example 2

Note: JavaScript following the synchronous and asynchronous callback function must ensure that the recommended method will be synchronous only but then again it depends on the requirement of the hour for programmers.

Conclusion

JavaScript is a well-formed and preferred language by most programmers because of its simplicity and flexibility so do JavaScript Functions when incorporated makes the entire coding experience user-friendly with respect to the view and browser compatibility. It lets users handle errors and can use the same function again and again without re-writing the entire function from scratch.