Skip to content

Blocking vs Non-Blocking

A blocking operation prevents further JavaScript execution until it completes. A non-blocking operation starts and allows the program to continue while it finishes in the background.

Without non-blocking operations, network requests, file reads, and timers would freeze the entire page, making the UI unresponsive.

flowchart TD
Request["User clicks button"]
Blocking["⛔ Blocking: UI frozen until response"]
NonBlocking["✅ Non-blocking: Spinner shown, UI responsive"]
Request --> Blocking
Request --> NonBlocking
// This blocks the entire thread for ~1 second
console.log('Start');
for (let i = 0; i < 1000000000; i++) {
// busy waiting — nothing else can happen
}
console.log('End');
// Output:
// Start
// (1 second pause — UI frozen)
// End
// This does NOT block — execution continues
console.log('Start');
setTimeout(() => {
console.log('Inside timeout');
}, 1000);
console.log('End');
// Output:
// Start
// End
// (1 second later)
// Inside timeout
ScenarioBlockingNon-Blocking
Network requestPage frozen for secondsSpinner shown, UI responsive
File readingApp hangsBackground processing
TimerCan’t run other codeOther code runs normally
  • Always use async APIs for I/O operations (network, file, database)
  • Offload heavy computations to Web Workers
  • Use setTimeout to yield to the event loop in long loops
  • Blocking: stops everything until complete
  • Non-blocking: starts operation, continues execution
  • All I/O should be non-blocking in production code