Holi Sale. Get upto 40% OFF on Job-oriented Training! Offer Ending in
D
H
M
S
Get Now
Exploring Node.js Code Execution Process

Exploring Node.js Code Execution Process

15 Feb 2024
Intermediate
19K Views
3 min read

By default, In Node.js all I/O operations like file I/O, database operations, network operations etc. are non-blocking. When these operations are completed, they are notified to the main thread using callback functions. You can also write blocking code for these operations using blocking version of Node.js functions which have Sync word as suffix.

Before going to explore Node.js code execution process, let's understand the blocking code and Non-blocking code with the help of example.

Blocking Code Example

The given code is a blocking code, since it will not continue the execution of the next lines of code until the read operation is completed.

var fs = require('fs');

//blocking code without callback function
var data = fs.readFileSync('text.txt','utf8'); //wait for reading
console.log(data);
console.log("Done!");

/* Output
 * Hello Node.js
 * Done!
*/

Read More - Node JS Interview Questions for Freshers

Un-blocking Code Example

The above blocking code can be converted into non-blocking code with the help of callback function. Hence, the given code will continue the execution of the next lines of code without waiting for read operation to be complete.

var fs = require('fs');

//un-blocking code with the help of callback function
fs.readFile('text.txt', 'utf8', function (err, data) {//doesn't wait and will read file asynchronously
 console.log(data);
});
console.log("Done!");

/* Output
 * Done!
 * Hello Node.js
*/

The Event Loop

Node.js execution model is based on JavaScript Event loop which is single threaded and based on JavaScript callback mechanism. The event loop facilitates the Node.js to handle concurrent requests. Let’s understand the Node.js execution model with the help of following diagram.

Node.js Event Loop

The steps of code execution are given below:

  1. Clients send theirs request to Node.js Server.

  2. Node.js Server receives those requests and places them into a processing Queue that is known as “Event Queue”.

  3. Node.js uses JavaScript Event Loop to process each client request. This Event loop is an indefinite loop to receive requests and process them. Event Loop continuously checks for client’s requests placed in Event Queue. If requests are available, then it process them one by one.

  4. If the client request does not require any blocking I/O operations, then it process everything, prepare the response and send it back to the client.

  5. If the client request requires some blocking I/O operations like interacting with database, file system, external services then it uses C++ thread pool to process these operations.

    Thread pool process steps are as:

    • Event loop checks for thread availability from internal thread pool. If thread is available, then picks up one thread and assign the client request to that thread.

    • Now, this thread is responsible for handling that request, process it, perform blocking I/O operations, prepare the response and send it back to the Event Loop.

    • Finally, the Event Loop sends the response to the respective client.

What do you think?

I hope you get the idea how Node.js code execution process work. I would like to have feedback from my blog readers. Your valuable feedback, question, or comments about this article are always welcome.

Take our free nodejs skill challenge to evaluate your skill

In less than 5 minutes, with our skill challenge, you can identify your knowledge gaps and strengths in a given skill.

GET CHALLENGE

Share Article
About Author
Shailendra Chauhan (Microsoft MVP, Founder & CEO at Scholarhat by DotNetTricks)

Shailendra Chauhan is the Founder and CEO at ScholarHat by DotNetTricks which is a brand when it comes to e-Learning. He provides training and consultation over an array of technologies like Cloud, .NET, Angular, React, Node, Microservices, Containers and Mobile Apps development. He has been awarded Microsoft MVP 8th time in a row (2016-2023). He has changed many lives with his writings and unique training programs. He has a number of most sought-after books to his name which has helped job aspirants in cracking tough interviews with ease.
Accept cookies & close this