Nodejs: Callback function and its concept

Nodejs has become widely popular due to its core factor “Asynchronism”. A callback is an asynchronous functional paradigm that often refers to be as “Higher-order function”. In Nodejs, most of the functions work as callback variants.

When an argument(callback function) in Nodejs is passed to another function, only the function definition is passed. This calling function mechanism is solely responsible for the timing of callback function execution. This is the sole basis of the asynchronous behavior of the Nodejs framework.

A callback function is a function which is:

  • Accessible by another function, and
  • It starts after the first function gets complete.

These functions in JavaScript will always pass as arguments to other functions. A callback function is called with arguments containing information that relates to the tasks:

  • data object
  • result object, and
  • error object

Node uses callback functions heavily so all the APIs of Nodejs are created in such a way to support callbacks.

For example, If a file I/O is complete, it will call the callback function while passing the callback function. And the remaining content of the file as a parameter. So there is no wait for File I/O. Such an execution property called non-blocking makes Nodejs highly scalable.

Thus, enabling it to process a high number of requests without waiting for any function to return results.

BLOCKING FUNCTION

Unlike the asynchronous function, the synchronous function blocks all the executions that require to complete a specific task. In simple, blocking function blocks the execution until the task on the resource is completed. So, that was the reason why the synchronous function refers to be a blocking function.

NODEJS NESTED CALLBACK FUNCTION

Nodejs nested callback function is a JavaScript function where it can be nested within other functions. A nested function or inner function can access the arguments and variables of an outer function where it is nested within. You can nest the callback function if any need for multiple operations to be done asynchronously.

EXAMPLES: NodeJS FUNCTIONS

With a few sample codes, we shall see how the callback function works in comparison with a blocking function, on reading (task) a file (resource) using the following examples:

  • Blocking Function
  • Callback Function
  • Nested Callback Function

BLOCKING FUNCTION

Following is an example node script that reads a sample.txt file synchronously, with the help of blocking function.

EXAMPLE:

When the above program runs, the terminal output will be as follows:

Terminal output

In the above example, fs.readFileSync is blocking the execution flow. After readFileSync statement is executed only after reading the file is completed.

NODEJS CALLBACK FUNCTION

A node script which reads a sample.txt file, with the help of Nodejs callback function, asynchronously as shown in below:

EXAMPLE:

When the above program runs, the terminal output will be as follows:

Terminal output

You may observe that even after executing console.log, it has not blocked the execution. Instead, a new process is started in parallel with the main control flow, to read the file (perform the task on the resource).

NODEJS NESTED CALLBACK FUNCTION

We can understand the Nodejs nested callback function with below sample coding. For example, we shall consider renaming a file and then deleting it using asynchronous functions.

EXAMPLE:

When the above program runs with the node, the output will be as follows:

Terminal output

In this blog, we have learned the basics of Nodejs callback functions by covering the following topics,

  • Execution flow of Callback functions with example.
  • How they are non-blocking with example.
  • How to use nested callback functions with example.

FINAL WORDS

Thanks for coming all this way to read about Nodejs callback function and its concept. I hope you have gained the overall callback function concept in Nodejs.

If you still have any queries regarding the about Nodejs and it’s developmental ideas, please feel free to Contact us for zero-cost consultation today.

Ecommerce consultant

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store