Skip to main content

What the term non-blocking means

The term «non-blocking» means that program execution does not stop while a long operation is being awaited, for example a network request, a timer or a file read. Put simply, non-blocking means «do not wait, carry on running other code».

Theory

TL;DR

  • Non-blocking code does not pause the main thread of execution.
  • While an asynchronous operation runs «in the background», JS performs other tasks.
  • It is implemented through the single threaded model, the Event Loop, asynchronous APIs and task queues.
  • setTimeout, fetch and fs.readFile are non-blocking operations.
  • A heavy synchronous loop, on the contrary, blocks everything, including the interface.
  • Node.js is built around non-blocking I/O, which is why one process serves many requests.

Quick example

javascript
console.log('A'); setTimeout(() => { console.log('B'); }, 2000); console.log('C');

Output:

javascript
A C B

setTimeout is a non-blocking operation: it does not stop the program, it schedules the callback for later.

Blocking and non-blocking code

The blocking version:

javascript
function sleep(ms) { const start = Date.now(); while (Date.now() - start < ms) {} // blocks the thread } console.log('A'); sleep(2000); console.log('B');

Output (after 2 seconds):

javascript
A B

Everything «froze» for two seconds: while sleep() runs, neither the interface nor other scripts execute. This is blocking code.

The non-blocking version with setTimeout (see the quick example above) prints A, C, B: the line C runs immediately, while B waits its turn without occupying the thread.

How it works under the hood

Non-blocking behaviour is achieved through:

  1. the single threaded model of JS, where everything goes through the Event Loop;
  2. asynchronous APIs (browser or Node.js ones);
  3. task queues (the callback queue), where finished results wait.

JS sends the operation (for example fetch) to an external API and does not wait, it keeps running the code. When the result is ready, the callback goes back into the queue, and the Event Loop runs it as soon as the call stack is free.

Non-blocking in Node.js

Node.js is built around non-blocking I/O. That means:

  • file, network and database operations are asynchronous;
  • instead of waiting for a result, Node keeps serving other requests.
javascript
const fs = require('fs'); console.log('Start'); fs.readFile('file.txt', 'utf8', (err, data) => { console.log('File has been read'); }); console.log('End');

Output:

javascript
Start End File has been read

Reading the file here is non-blocking: Node does not wait for it to finish, it runs the rest of the code.

Why this matters

ApproachWhat it doesDownside
BlockingRuns operations strictly one after another«Hangs» the thread, slows the interface down
Non-blockingLets other tasks run while waitingYou have to be comfortable with callbacks and promises

Asynchrony and non-blocking behaviour make JS responsive and efficient, especially in the browser and in server applications.

An everyday analogy: picture a waiter in a cafe.

  • The blocking approach: the waiter stands and waits until the order is cooked, and only then takes the next one.
  • The non-blocking approach: the waiter passes the order to the kitchen and serves other customers while it is being cooked.

JavaScript behaves like the second waiter.

Summary of terms

TermMeaning
BlockingExecution stops until the operation has finished
Non-blockingExecution continues and the result is handled later
ImplementationThrough the Event Loop, asynchronous callbacks and promises
BenefitDoes not «freeze» the thread, fast response

Common mistakes

  • Assuming non-blocking means multithreading: the code still runs in a single thread.
  • Writing your own sleep() on a while loop instead of await new Promise(r => setTimeout(r, ms)).
  • Using the synchronous Node.js API variants (fs.readFileSync) in code that serves requests.
  • Doing heavy computation on the main thread: even asynchronous code will not save you from blocking.
  • Thinking that await blocks the thread: it only pauses its own async function, the thread stays free.

Short Answer

Interview ready
Premium

A concise answer to help you respond confidently on this topic during an interview.