Open In App

What is the difference between returning a callback and just calling a callback ?

Last Updated : 12 Jan, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

In programming, a callback function is a function that is passed as an argument to another function, which executes the callback function whenever it requires to do so to complete a certain task. Callback function provides a variety of powerful programming techniques. Callback function makes function execution much more dynamic by allowing what kind of action we want to perform using callback.

Callback function are of two types: synchronous callback (blocking callback) and asynchronous callback (deferred callback).

  • Synchronous callback: They are also known as blocking callback function. They are synchronous in nature as their name implies i.e. they follow synchronous program execution which means that each line of code is executed only after previous line has completed execution and there is immediate execution. Thus, synchronous callbacks are invoked before a function completes execution. Example, C and C++.
  • Asynchronous callback: They are also known as deferred callback function. They maybe invoked after a function completed execution. They are used in the handling I/O operations and event based program execution. For example, sending an HTTP request to a server and waiting for a response. Here the response may be received late due to high network traffic, so it is asynchronous. Now suppose, we want execute a function upon receiving the response then we pass a callback to the function sending the request so it will automatically executing it after receiving the response, so it is asynchronous because it takes some time to receive the response and the execution is not immediate. Dynamic languages like JavaScript, Python, Lua allows passing callback functions directly as objects.

Now we know what are callbacks and their types, lets see the difference between returning a callback and just calling a callback.

Returning a callback: In returning a callback, the called function returns a callback instead of executing it immediately. The calling code where the callback is returned can execute the returned callback immediately, pass it to some other function and so on. Lets see an example of a function that returns a callback function.

Javascript




const returnCallbackFunction = () => {
    let i = 10;
    return () => console.log(
        'This is a callback function.\nValue of i is', i
    );
}
 
// A callback function is returned by
// the returnCallbackFunction()
let callbackFunction = returnCallbackFunction();
 
// CallbackFunction is executed
callbackFunction();


Output:

This is a callback function.
Value of i is 10

Calling a callback: In contrast to returning a callback function, calling a callback function is simply the immediate execution of the callback function. Lets see an example of calling a callback function.

Javascript




// Defining a callback function
const callbackFunction = () => {
    console.log('Calling a callback function');
}
 
// Calling the callback function
callbackFunction();


Output:

Calling a callback function


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads