Nodejs async engine in action (visually)

Valerio Barbera

Hi, I’m Valerio, software engineer and CTO at Inspector.

Whether you’ve looked at async/await and promises in javascript before, but haven’t quite mastered them yet, this article aims to help you to better understand the real effects of the nodejs async engine on your code execution flow.

Furthermore we’ll do it visually, navigating the code execution flow in real-time thanks to Inspector. At the end of the article you’ll find all the resources to quickly connect your nodejs application with Inspector.

Synchronous Programming

In traditional programming languages, most operations happen synchronously. If you think about PHP, and about how you would read a file using PHP, you would end up with something like this:

echo "Start reading the file...";

$content = file_get_contents('./export.csv');

echo $content;

echo "End of the script...";

The main thread will be blocked until the file is read, which means that nothing else can be done in the meantime, so we are sure that the script will echo the content of the file.

When you execute a synchronous script, you wait for each task to finish before moving on to another task.

Start reading the file...
#Content of the file
End of the script...

As you can see in the image below, this behaviour become clear taking a look to the code execution timeline of a Laravel application. The endpoint below runs a bunch of query against the database and the redis server, and each task has been executed sequentially, one after the other.

When a task is finished another one is executed until the end of the program.

Inspector Timeline
Code Execution flow visualization in Inspector.

Asynchronous programming

In an asynchronous environmet like Nodejs some tasks can be scheduled to be executed in parallel with the main script, leaving the main program to continue running subsequent tasks in the script.

Take a look on the following code example to read a file in nodejs:

const fs = require('fs')

console.log("Start reading the file...")

fs.read('./export.csv', function(err, content) {
    console.log(content)
})

console.log("End of the script...")

We tried to read a file using the asynchronous interface of the fs module. It works as expected – the content variable will be printed out after “End of the script…”.

Start reading the file...
End of the script...
#Content of the file

Once we started to read our file the execution continued, and the application printed End of the script.... Our callback was only called once the file read was finished.

We can see the parallel execution in anction using Inspector to have a visual representation of the code execution flow in an async context:

Nodejs execution flow

Where needed the tasks are executed simultaneously, each with its own duration. Code Execution Monitoring often allows us to become more aware of the behavior of the code we write. That’s why its adoption continue to grow in the Nodejs and Laravel communities.

To understand the async nature of NodeJs more in-depth, you need to absolutely watch this video:

Next Up: Your First Node.js Server

In the next chapter, you will learn how to deploy your Node.js server using Laravel Forge – in the meantime if you have any questions, don’t hesitate to ask!

Related Posts

Code Execution Monitoring for Symfony applications using Inspector

Hi, I’m Valerio software engineer from Italy and CTO at Inspector. In this article I’ll show you how to measure performance and stability of your Symfony application in the production environment. Symfony is one of the most used PHP frameworks in the world, so I hope this implementation can bring real benefits in many developers’

How and why we implemented the “Repository Pattern” in our PHP backend

Hi, I’m Valerio, CTO and founder at Inspector. In this article I talk about the Repository Pattern and how we implemented it in our application to solve a scalability problem. The Repository Pattern is one of the most disccussed patterns due to a lot of conflicts with ORMs. This pattern is often intended as an

How I handled the scalability of the SQL database in Inspector

Hi, I’m Valerio software engineer, CTO and founder at Inspector. In this article I’ll talk about what I learned trying to increase the operational limits of the Inspector SQL database. Before talking about read-replicas or sharded-data, it might be helpful to introduce the problem to solve and the most common strategies to improve the ability