Member-only story

NodeJS Interview and Prep: What is blocking vs. non-blocking?

Marika Lam
2 min readJun 19, 2024

--

Blocking

  • Blocking is when the execution of additional JavaScript in the NodeJS process must wait until a non-Javscript operation completes. This happens because the event loop is unable to continue running Javascript while a blocking operation is occurring.
  • Blocking methods execute synchronously.
  • Synchronous execution usually refers to code executing in sequence.
  • Using the file system module as an example, this is a synchronous file read.
const fs = require('node:fs');
const data = fs.readFileSync('/file.md'); // blocks here until file is read
  • Blocking refers to operations that block further execution until that operation finishes.
  • Below, fetch is a non-blocking operation as it does not stall alert(3) from execution
// Blocking: 1,... 2
alert(1);
var value = localStorage.getItem('foo');
alert(2);
  • An example of synchronous, blocking operations is how some web servers like ones in Java or PHP handle I/O or network requests. If your code reads from a file or the database, your code blocks everything after it from executing. In that period, your machine is holding onto memory and processing time for a thread that isn’t doing anything!

--

--

No responses yet