×

Node.js Callback Concept

Understanding the Concept of Callback

Callback is an asynchronous equivalent for a function that is called at the completion of a task. The Callback helps to avoid any I/O blocking and allows other code to execute in the period in-between. Node.js provides an Asynchronous platform that doesn’t wait for finishing the file I/O, which implies that Node.js uses Callbacks, making the Node.js platform highly scalable.

Blocking vs Non-Blocking

The operations that block any further execution until the current operation completes, are referred as Blocking. However, the operations that don’t block any execution are referred as Non-Blocking. As per the Docs of Node.js, the Blocking in Node.js happens when the execution of an additional JavaScript process waits until a Non-JavaScript process ends, which implies that Blocking methods works Synchronously; whereas Non-Blocking uses Asynchronous method with the help of Callback function.

Let’s see some examples given below that are based on the fundamental concept of Callback.

Blocking Example

File: blocking.js  

// Blocking
// Importing the fs module
const fs = require('fs');
console.log("This is an Example of Blocking");
// Reading the content of a file
var text = fs.readFileSync('./mytext.txt');
console.log("Content of the File: " + text.toString());
console.log("Program Ended!!");

To execute the above program, type the following command on the terminal:

$ node blocking.js

The Output is as follows:

This is an Example of Blocking
Content of the File: Hello! Welcome to tutorialandexample.com
Learning is so easy with tutorialandexample.com
Program Ended!!

Non-Blocking Example

File: non-blocking.js

// Non-Blocking
// Importing the fs module
const fs = require('fs');
console.log("This is an Example of Non-Blocking");
// Reading the content of a file
fs.readFile('./mytext.txt', function ( err, text ){
    if ( err ){
        return console.error(err);
    }
    console.log("Content of the File: " + text.toString());
});
console.log("Program Ended!!");

To execute the above program, type the following command on the terminal:

$ node non-blocking.js

The Output should is given as follows:

This is an Example of Non-Blocking
Program Ended!!
Content of the File: Hello! Welcome to tutorialandexample.com
Learning is so easy with tutorialandexample.com

From the above script files and their outputs, we can conclude that the execution of code in the Blocking program is done in order. However, the execution of code in the Non-Blocking program is not done in a sequence. The execution of other codes doesn’t wait for the completion of the code to being executed.


Related Topics

Node.js NPM

Node.js – Node Package Manager NPM is an abbreviation for Node Package Manager. It is the default package manager developed by Isaac Z. Schlueter using JavaScript Programming Language, designed for JavaScript Runtime...

7 minutes read.

Node.js Error Handling

Node.js – Error Handling: Errors are part of every programming language, and Node.js Environment is not an exception. There are many predefined errors in Node.js, including errors like SyntaxError, RangeError, and...

4 minutes read.

Node.js Core Debugger

Node.js – Core Debugger: It’s always a challenge when it comes to debug an application. The Asynchronous workflows of Node.js include an additional layer of intricacy to this difficult process....

3 minutes read.

Difference Between Node.js and PHP

Node.js vs PHP Node.js and PHP both are the powerful back-ends to create versatile websites. Even when these two technologies fall under the same category, their features are pretty diverse. There's...

6 minutes read.

Node.js Path Module

Understanding the path Module in Node.js The path module is a built-in module of Node.js that provides various methods to interact with the files and directory path names on the device’s...

6 minutes read.

Node.js Event Loop Phases

Node.js – Phases of Event Loop Once we have understood the Node.js event loop concept and the basic introduction to its phases, let's discuss the functions of the Check handler phase...

4 minutes read.

Node.js File System

Introduction to File System in Node.js FS, abbreviated as File System, is a Node.js Build-in Module, which provides an Application Programming Interface (API) to interact with the File System, and to...

15 minutes read.

Node.js Events Module

Introduction to the Node.js events Module As we’ve already discussed, Node.js is a JavaScript runtime with an event-driven architecture designed to perform Asynchronous tasks. In order to accomplish the event-driven architecture,...

4 minutes read.

Node.js OS Module

Introduction to Node.js OS Module The OS Module in Node.js provides the Operating System-related Utilities. This module delivers the functions to interact with the Operating System. It also delivers the Operating...

5 minutes read.

Node.js Discovering Timers

Node.js Discovering Timers: Timers are multi-lingual, and much functional in countless use cases. The Timers are mostly used in many popular programming languages, including Node.js. However, it's pretty simple to...

4 minutes read.

Node.js REPL

Node.js REPL: REPL represents the Read Eval Print Loop. REPL is modest; much more, an interactive software development environment similar to the command line shells used in various Operating Systems....

4 minutes read.

Difference Between Node.js and Django

Node.js vs Django In Web Application Development, we must have a robust technology to provide strength to our Web application. Node.js and Django both are pretty commanding technologies that help us...

6 minutes read.

Node.js Callback Concept

Understanding the Concept of Callback A Callback is an asynchronous equivalent for a function that is called at the completion of a task. The Callback helps to avoid any I/O blocking and allows other code to execute...

2 minutes read.

Node.js Console

Node.js Console: Like the JavaScript console mechanism, a console module is designed for Node.js that provides a debugging console for writing or printing messages. Usually, in JavaScript, a console mechanism is...

4 minutes read.

Node.js Streams

An Introduction to the Streams Streams are one of the elemental concepts that are introduced to power the applications based on Node.js. These are the method used to handle the data...

6 minutes read.

Difference Between Nodejs Angular and React

A Right JavaScript Framework Within a business, it is necessary to pick the right technology. As much as this applies to the web application and software development, it also applies in...

5 minutes read.

Node.js Modules

Node.js Modules: At the point when we'll begin composing Node.js applications, we would place all our code into one big node.js file, regardless of how enormous or complex our application...

4 minutes read.

Differentiation of Web Scraping and Web Crawling

Most of us get confused between the word’s web scraping and web crawling, and even some people think they are synonyms for each other. But there is a considerable difference between...

3 minutes read.

Node.js Buffers

An Introduction to Buffers A Buffer is an area in memory (typically RAM) that is used to store binary data. Binary is a collection or a set of 0 and 1....

4 minutes read.

Node.js HTTP Module

An Introduction to HTTP Module The http module is a core module in Node.js that uses the Hypertext Transfer Protocol (HTTP) to transfer data. With the help of the http module,...

7 minutes read.