Programming

Callback Function In Node.JS

Jugal Kamdar
5 min readJun 15, 2021

A look into what is a callback function, how it works and used

Whats a callback function? Photo by Eduardo Sánchez on Unsplash

A callback function is a function passed as an argument to another function with an intention to be called at a later time.

A callback function is a common and essential feature of node.js. It is used extensively in JavaScript libraries and frameworks. They are used both in asynchronous and synchronous programming.
Callback functions are possible in JavaScript because of their first-class citizen treatment to functions in it.

Topics to be covered with examples are:
1. Named function passed as a callback.
2. Anonymous callback function.
3. Synchronous and Asynchronous callback function.
4. Create a function that accepts the callback function as an argument.

Let us start with simple examples to get to know more about callback functions.

Note:- These examples use the ES6 Arrow function method.

1. Named function passed as a callback

named-callback-function
named-callback-function

Here, we have declared a function with a name message and passing it as a callback function to setTimeout function. setTimeout is a built-in Node.JS function that accepts two arguments:
1. Callback function.
2. Time to delay the execution in a millisecond.

So, we pass the message function as the first argument and setting 3-seconds time delay as the second argument. Once the 3-second timer completes,setTimeout function will execute the callback function.

Output: Callback function example passed as name to argument

2. Anonymous callback function

The anonymous function is a function without a name that can be passed as an argument to another function. It is passed as an argument in two ways to the callback function.

anonymous-callback-function-1
anonymous-callback-function-1

Here, we assign the anonymous function to the message variable so that it can be used later. Then the variable message is passed as the first argument to setTimeout function and 3000 milliseconds as the second argument for the time delay.

anonymous-callback-function-2
anonymous-callback-function-2

Here, we pass the anonymous function directly as an argument for the callback function.

The output is similar for both examples.

Output: This is an anonymous callback function example

Note:- We will be using the second way for the rest of our examples.

3. Synchronous and Asynchronous callback function

As said earlier, the callback function can be synchronous and asynchronous. When synchronous, the program needs to execute its operations sequentially, where the next operation will run only after the current one has completed.

While asynchronous, the program does not execute sequentially so, the program would not wait for the current operation to complete. Let us discuss it with examples.

First, let us start with the synchronous callback function example.

synchronous-callback-function
synchronous-callback-function

In the above example, we declare the variable number and assign an array to it. Then we call the forEach function to iterate array stored in number variable.
Here, we pass the callback function to the forEach function as an argument, these callback function prints the number returned by the forEach function. Once the forEach function completes its process, only then it can print the console.log() message after singleNumber .

Output: 
1
2
3
4
5
Function executes only after singleNumber function, following synchronous way.

Now let us see the asynchronous callback function example.

asynchronous-callback-function
asynchronous-callback-function

Here, we define the setTimeout function which is a node.js provided asynchronous API. It takes two arguments which is the callback function and the number of milliseconds to delay the function.

When the above program executes, the setTimeout function gets executed and is delayed for 4-seconds but, the program does not wait for the setTimeout function to complete instead, it continues to the next operation that is the console.log() message after the setTimeout function.

Once the delay time is over, the setTimeout function places the callback function into the callback queue to get processed.

Output:This message prints before setTimeout message, following asynchronous way.
This message will print after 4 seconds.

4. Create a function that accepts the callback function as an argument

accept-callback-function
accept-callback-function

In this example, we created a function that accepts callback function as an argument and calls it back to process.

Here, we have defined the function add which accepts three arguments that are:
1- number value
2- number value
3- callback function

When we run the program the add function gets executed as we call it and pass the number values and a callback function as an argument. Then, the add function waits for the delay time of setTimeout function to complete. Once completed, the callback function is executed and prints the sum.

Summary

So far, we talked about callback functions, how to implement them and creating a function that accepts a callback function. It is now clear that callback functions are one of the essential and powerful features of JavaScript. It is often used when writing asynchronous node.js code. By using the callback function makes our code clean and readable, needless to repeat, and maintainable.

That’s it, folks! The End.

Thank you for reading!

It is my first time to write an article and publish it on Medium. Please comment if anything needs to be improved.

I hope you found this article helpful and informative? If yes, give it a 👏, share it and tweet it 💬 to reach the broader community.

Follow me Jugal Kamdar on Medium and check out my upcoming articles.

--

--

Jugal Kamdar
Jugal Kamdar

Written by Jugal Kamdar

Software Developer | Ethereum | Solidity | Python | Node.JS

No responses yet