How node.js prevents blocking code ?
Last Updated :
25 Jun, 2021
Node.js is a cross-platform JavaScript runtime environment that helps to execute and implement server-side programs. Node is assumed to prevent blocking code by using a single-threaded event loop. In this article, we will discuss this event loop and how it asynchronously implements functions by using callbacks.
Blocking and Non-blocking operations: Blocking operations refer to the pieces of code that block the execution of other code until they are completed. While non-blocking operations allow further pieces of code to execute without making them wait and use callbacks when they are completed.
Thus, blocking code can be said to work synchronously while non-blocking code works asynchronously. While discussing non-blocking code, we come up with a term called callback. The callback is a function that is invoked when a process completes its execution and wants to continue its normal execution with the outer function.
Node and Event Loop: Now we know what blocking and non-blocking operations are, we can discuss how node prevents blocking code. Node uses a single thread, which means one task can be executed at a time. This is done by using a stack. While reading the code from top to bottom, each instruction is pushed into a stack and when its execution is completed, it pops out of the stack. Now we may come across an instruction/function that will take a longer time to execute which can result in the delay in popping the stack and execution of further statements.
So what Node.js allows is the use of Event Loop. Each time when we encounter such a situation, the process causing the delay is offloaded from the stack and the execution of that process continues parallel to further execution of the main code. Thus, the callback for that function is pushed into a task queue and the code continues to execute asynchronously. When the process completes its execution, the callback function returns the desired output from that process and resumes normal execution.
Example: Let's consider an example that will demonstrate how the event loop works.
index.js
<script>
// Simple JavaScript Code to show Event
// loop demonstration for Node
console.log("Geeks");
// Printing Geeks after 3 seconds
setTimeout(function cb() {
console.log("Geeks");
}, 3000);
console.log("For");
</script>
Run the index.js file using the following command.
Output:
Geeks
For
Geeks
Explanation: Although the instruction to log Geeks is before the instruction to log For, that is not the order they are getting logged in the console. What happens is when the setTimeout function is called, the timer to wait for 3 seconds is triggered. But this timeout will not happen while the function is in the call stack, and instead is handled by Node API. Thus, further instructions get processed as they should and when the timer turns to zero, the callback function will be invoked and the result from the function will be returned.
References: https://nodejs.org/en/docs/guides/blocking-vs-non-blocking/
Similar Reads
How Node.js works behind the scene ? Node.js is the JavaScript runtime environment which is based on Googleâs V8 Engine i.e. with the help of Node.js we can run the JavaScript outside of the browser. Other things that you may or may not have read about Node.js is that it is single-threaded, based on event-driven architecture, and non-b
5 min read
Non-Blocking event loop in Node.js Node.js operates on a single-threaded, event-driven architecture that relies heavily on non-blocking I/O operations to handle concurrent requests efficiently. This approach is enabled by its event loop mechanism, which allows Node.js to handle multiple requests concurrently without creating addition
5 min read
How to handle concurrency in Node.js ? Node.js is an open-source, cross-platform runtime environment built on Chrome's V8 Engine. It is used to develop highly scalable backend as well as server-side applications. Node.js uses a single-threaded event loop architecture. It is also asynchronous in nature. These are the two main reasons why
4 min read
Blocking and Non-Blocking in NodeJS In NodeJS, blocking and non-blocking are two ways of writing code. Blocking code stops everything else until it's finished, while non-blocking code lets other things happen while it's waiting. This difference is key to understanding how NodeJS works and writing fast, efficient applications.What is B
6 min read
How Node.js overcome the problem of blocking of I/O operations ? Node.js uses non-blocking I/O, the mechanism that allows you to have a single thread of execution running your program. If Node.js had to use blocking I/O, you wouldn't be able to do anything else while waiting for an I/O to complete. Below is an example image of what happens when Node.js needs to u
7 min read