Blocking and Non-blocking

Updated on Oct 5, 2023

Since Node.js is a runtime environment, which we discussed in a previous part of this tutorial, its primary purpose is to execute JavaScript. We also know that it is single-threaded, and we will add one more element to Node.js’ core: it is non-blocking. That seems like another phrase people just starting off with Node.js may not know what it means. It is actually easy to explain, so read along.

We must first explain what blocking is in the context of Node.js and programming in general. When an application is running, it spawns a process. Then, when it has to execute its JavaScript, it makes a request, or a call, to the server it is hosted on, be it locally on your computer or externally on a provider. Then, the server has to process that request and return the necessary output for the executed script. That is how many programs work: a request is made, then an output is returned. However, where blocking and non-blocking come into play is what happens when the application is waiting for the output by the server. It is relatively simple.

  • Blocking - This type of call occurs when the Node.js process will not execute any additional JavaScript until the non-JavaScript portion of the call receives the server’s output. Only then will the JavaScript execute, and the application will proceed. This type of execution is synchronously, meaning line by line. Computer systems often read code from top to bottom, one line at a time, without skipping lines unless specifically instructed. While such a method of execution may have its advantages, it is not optimal, and that is why the majority of Node.js’ scripts and modules are non-blocking;
  • Non-blocking - This type of call is the exact opposite of the above one and is what Node.js uses most of the time. These calls will not halt the JavaScript from executing until the non-script processes finish. Instead, the program will execute all its operations asynchronously, meaning the line-by-line method described above will not apply. Often, the application will call for the function to simply keep going after starting an operation without waiting for the output. As you can imagine, this is a much smoother way of handling the process of interacting with the hosting server.

We wanted to introduce you to this vital distinction, as it is one of the most essential features of Node.js. Despite being single-threaded, it can handle all of its processes very efficiently, which attributes to the speed of its applications. The Node.js team has in-depth documentation about the difference if you want to learn more about it.

On this page...

    Node.js Hosting

    • Multiple Node Versions
    • 24/7 Node.js Support
    • Free Domain Transfer
    • Hack-free Protection
    • Fast SSD Storage
    • Free Node.js Transfer
    • Free Cloudflare CDN
    • Immediate Activation
    View More