Node JS and Non-Blocking & Blocking IO

Code 360 by Coding Ninjas

Node js is Run time environment of java-script. Which execute java-script in back-end or in server side with the help of chrome vs-8 engine. What, what thing makes special node js the ans is . Non-blocking Event driven system.

So Let's understand first that what is Blocking system.

The word blocking means block the further code in order to execute the current one. In simple terms while machine is running one piece of code until that particular code is not get executed fully other code part will not going to run

that is blocking code. Also known as Synchronous programming language.

  // File module.
const fs = require('fs');
const filePath  = 'file.txt';

console.log('Before the Blocking');

const data =  fs.readFileSync(filePath,'utf-8');
console.log(data);

console.log('Blocking Code');

In the above program codes executes line by line. Means if any chance the middle code not executed than remaining part is not going to run. This problem is solve by Asynchronous.

The code looks simple and straight forward but it has the disadvantage of the third line blocking the execution of the additional javas-cript code. Imagine if we have a super large file that can take several seconds to read, then the execution of the additional java-script code will have to wait for the data to finish reading.

Non-blocking operations

Node.js libraries are written using an asynchronous, non-blocking paradigm.

In non blocking operation other code is executed without disturbing above code

that is the core functionality of node js . That's why several online streaming platform use node js as server side including.Netflix, NASA, Trello, PayPal, LinkedIn, Walmart, Uber, Twitter, Yahoo, eBay, GoDaddy.

const fs  = require('fs');
const filePath  = 'file.txt'; 

console.log('Before the file.');

const AsyFile  = (filePath)=>{
  fs.readFile('file.txt','utf-8',(err,data)=>{
    if(err){
        console.log('Something went Wrong :( ')
    }else{
        console.log(data);
    }
  })
}

// function calling..
AsyFile();

console.log('After the file execuation ');

This happened because the fs method readFile performs an asynchronous operation and does not block the execution of other java-script code but instead, it accepts a function (popularly referred to as a callback function) as it the third argument and calls that function with the data when it’s done reading the text file or calls it with an error if it encounters an error.

Node.js runs like a single lane highway, but it's super good at handling traffic. Here's why:

  • One lane: It uses a single thread for code, keeping things simple.

  • Fast cars: I/O operations don't block the lane, they zoom off on their own.

  • Traffic cop: The event loop keeps things moving, calling functions when I/O is done.

This lets Node.js handle many requests at once, making it great for beginner-friendly web apps!

Thank you for reading my content. Be sure to follow and comment on what you want me to write about next 🤓.

Did you find this article valuable?

Support Saurabh verma by becoming a sponsor. Any amount is appreciated!