Skip to main content

Command Palette

Search for a command to run...

Blocking Code VS Non-blocking code in Node.js

Updated
3 min read
Blocking Code VS Non-blocking code in Node.js

Let’s learn about the blocking and non-blocking code in node.js in such a simple way as talking over a cup of tea ( or coffee as per your call).

Imagining a scenario and then learning would be effective for understanding this concept, so imagine you run a tea-stall as a single-person chai stall you serve one customer at a time —> You:

  1. Take order ☑️

  2. Make chai (takes time) 🍵

  3. Serve it ✔️

  4. Then go to the next customer.

if one tea takes total 5 minutes to serve then all customers need to wait for their turn , and each will be served 5 minutes later than the customer before them. This will create long queue , wastage of time in other words inefficient work.

🔒 Blocking Code Example:

const fs = require('fs');

console.log("Starting to read file...");

// Blocking read
const data = fs.readFileSync('sample.txt', 'utf8');

console.log("File content:", data);
console.log("Done reading file.");

What happens?

  • Node will stop everything else until it finishes reading the file.

  • If the file is large, your whole app pauses.

Non-Blocking Code: Multitasking Chaiwala

Now imagine you're a smart chaiwala with a helper.

You:

  1. Take an order ☑️

  2. Tell helper to make chai

  3. Meanwhile, take another order!

  4. Helper says: “Chai is ready!”

🔓 Non-Blocking Code Example:

jsCopyEditconst fs = require('fs');

console.log("Starting to read file...");

// Non-blocking read
fs.readFile('sample.txt', 'utf8', (err, data) => {
    if (err) {
        console.error("Error reading file:", err);
    } else {
        console.log("File content:", data);
    }
});

console.log("Done requesting file read.");

What happens?

  • Node sends the file-read task aside and keeps going.

  • When the file is ready, it calls back and logs the content.

Why This Matters in Node.js?

Node.js uses a single-threaded event loop — like one brain doing many tasks by delegating them smartly.

If you block the brain (blocking code), everything stops. If you delegate tasks like reading files or making network requests (non-blocking), your app stays fast and responsive.

FeatureBlocking CodeNon-Blocking Code
Execution StyleWaits until task is doneContinues and uses callback/promise
PerformanceSlower if tasks are longFaster and scalable
Examplesfs.readFileSync, while loopfs.readFile, setTimeout, Promises

Conclusion

Understanding the difference between blocking and non-blocking code is essential when working with Node.js. Blocking code pauses the entire program until a task is completed, which can make your application slow and unresponsive—like a chaiwala serving one customer at a time. In contrast, non-blocking code allows your application to handle multiple tasks concurrently, boosting performance and responsiveness—like a multitasking chaiwala with a helper!

Node.js thrives on non-blocking, asynchronous code, making it ideal for building fast, scalable, and real-time applications. By embracing non-blocking techniques such as callbacks, promises, and async/await, you can ensure your apps stay efficient and smooth even under heavy load.

So, choose wisely—and let your Node.js app serve everyone their chai on time! ☕