Blocking and NonBlocking in NodeJS (original) (raw)

Last Updated : 25 Apr, 2026

In Node.js, blocking code waits for a task to complete before moving forward, while non-blocking code allows other operations to proceed without waiting.

Blocking in NodeJS

When your code runs a task, it stops and waits for that task to completely finish before moving on to the next thing. It's like reading a book one word at a time, never skipping ahead.

function myFunction() { console.log("Starting a task..."); // Simulate a long-running task (blocking) let sum = 0; for (let i = 0; i < 1000000000; i++) { // A big loop! sum += i; } console.log("Task finished!"); return sum; }

console.log("Before the function call"); let result = myFunction(); console.log("After the function call"); console.log("Result:", result);

`

**Output:

blocking code output

Blocking Operation in NodeJS

**Non-Blocking in NodeJS

Non-blocking means your program can keep doing other things while a task is running in the background. It doesn't have to stop and wait for that task to finish before moving on.

function myFunction() { console.log("Starting a task...");

// Non-blocking I/O operation (file read)
const fs = require('fs');

fs.readFile('file.txt', 'utf8', (err, data) => {
    if (err) {
        console.error("Error reading file");
        return;
    }

    console.log("Task finished!");
    console.log("File content:", data);
});

}

console.log("Before the function call"); myFunction(); // The program doesn't wait here console.log("After the function call");

`

**Output:

Screenshot-2026-04-14-152823

Real-World Examples

Let's see how blocking and non-blocking I/O work with web servers. This will show why non-blocking is so much better, especially in NodeJS.

Blocking HTTP Server

A blocking server handles one request at a time. If it's busy reading a file for one user, other users have to wait.

const http = require('http');
const fs = require('fs');
http.createServer((req, res) => {
const data = fs.readFileSync('largeFile.txt', 'utf8'); // Blocking!
res.end(data);
}).listen(3000);

console.log("Server running at http://localhost:3000/");

Non-Blocking HTTP Server

A non-blocking server can handle many requests at the same time. If it needs to read a file, it starts the read and then goes back to handling other requests. When the file is ready, the server is told about it and then it can send the data to the user.

const http = require('http');
const fs = require('fs');

http.createServer((req, res) => {
fs.readFile('largeFile.txt', 'utf8', (err, data) => { // Non-blocking!
if (err) {
res.writeHead(500);
res.end("Error reading file");
return;
}
res.end(data);
});
}).listen(3000);

console.log("Server running at http://localhost:3000/");

Understanding Non-Blocking I/O in Node.js

NodeJS handles non-blocking I/O using

Handling Concurrency and Throughput in Node.js

Node.js is single-threaded but achieves concurrency using the event loop, which handles I/O operations efficiently and executes callbacks once tasks are completed.

Difference Between Blocking and Non-Blocking

Here's a table which shows the key differences between blocking and non-blocking operations

Blocking Operations Non-Blocking Operations
Waits until the operation completes. Continues immediately; operation completes later.
Current thread is blocked during execution. Current thread continues; operation handled separately.
Easier to implement. Requires callbacks, promises, or async/await.
May waste resources while waiting. Uses resources more efficiently.
Can cause delays if operations are slow. Keeps application responsive during long operations.
Suitable for simple tasks where waiting is acceptable. Suitable for high-responsiveness tasks like APIs and real-time apps.
Example: Reading a file completely before proceeding. Example: Starting a file read and continuing without waiting.
Errors handled immediately after execution. Errors handled later, often in callbacks or error handlers.