Learn Simpli

Free Online Tutorial For Programmers, Contains a Solution For Question in Programming. Quizzes and Practice / Company / Test interview Questions.

What is asynchronous node js with example

In this chapter, you will learn what is asynchronous node js with example. Asynchronous node js can perform more than one task at a time without waiting for any IO operation.

Asynchronous node js

IO operation is an operation that is related to the database or reading some external APIs. That is how the Blocking and Non-Blocking execution approach comes to the picture.

Asynchronous or Non-Blocking approach:
When any task that can be executed without waiting for any IO operations comes under Asynchronous or Non-Blocking approach.

Let’s take a real-world example,
In other words, it’s like a waiter in the hotel, who is accepting the orders from the customer and pass the order to kitchen room and he never waits for food from kitchen room, instead, he goes to the other customer and accepts the order. In the meanwhile he will be checking if previous order items are been prepared or not, if prepared he will serve that to the customer.

Technically the above task can be executed in node js by call back functions with the single-threaded concept. Where a thread can accept the requests from customers and pass the request to the event loop with a callback. The event loop is the mean heart of the Asynchronous or Non-Blocking approach that manages the all-timers, callback so on.

Synchronous or Blocking approach:
When any task that waits for some of the IO operations and blocks the other task comes under the Synchronous or Blocking approach.

Let’s take a real-world example,
In other words, it’s like a waiter in the hotel, who accepts the orders from the customer and goes to the kitchen room with the order. He waits at the kitchen room until the food is been prepared, he won’t accept any new customer order. The time he is been waiting for the food until its get ready, can be said as blocking operation.

Example of reading the file in the synchronous approach

Create the file users.json and put the content

{
    "users": [
        {
            "id":1,
            "name":"John"
        },
        {
            "id":2,
            "name":"Jack"
        }
    ]
}

Create a file: serverSync.js put the below code
Run the command: node serverSync

// reading the file synchronously
const readFile = (file) => {

    const fileData = fs.readFileSync(file, 'utf8');
    if (fileData) {
        console.log('Reading the file has been finished');
        console.info(fileData);
    } else {
        console.log('File is empty....');
        return false;
    }
}

// greet employee
const greatEmployee = (param) => {
    console.log(param);
}
console.log('Requesting the read opration');
readFile('users.json');
console.log('Waiting for response to serve next request');
greatEmployee('Hi John, How are you?');

// output
Requesting the read operation
Reading the file has been finished
{
    "users": [
        {
            "id":1,
            "name":"John"
        },
        {
            "id":2,
            "name":"Jack"
        }
    ]
}
Waiting for response to serve next request
Hi John, How are you?

Example of reading the file in the asynchronous approach
Create a file: serverAsych.js put the below code
Run the command: node serverAsych

// reading the file synchronously
const readFile = (file) => {

    const fileData = fs.readFileSync(file, 'utf8');
    if (fileData) {
        console.log('Reading the file has been finished');
        console.info(fileData);
    } else {
        console.log('File is empty....');
        return false;
    }
}

// greet employee
const greatEmployee = (param) => {
    console.log(param);
}
console.log('Requesting the read opration');
readFile('users.json');
console.log('Ready to serve next request');
greatEmployee('Hi John, How are you?');

// output
Requesting the read operation
Ready to serve next request...
Hi John, How are you?
Reading the file has been finished
{
    "users": [
        {
            "id":1,
            "name":"John"
        },
        {
            "id":2,
            "name":"Jack"
        }
    ]
}

 

One thought on “Asynchronous node js

Comments are closed.