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

DAZN platform down on sunday

Last Sunday, DAZN (one of the most known streaming platforms in Europe) crashed and subscribers were unable to regularly watch the portal’s flagship product: FOOTBALL. The result was an apology post published on the DAZN facebook page (you can see it below) in which a supplier was blamed, and customers left with no alternatives, so

How to deploy a NodeJs server using Laravel Forge

Hi, I’m Valerio, software engineer from Italy, and C.T.O. at Inspector. We recently worked to replace the http handler behind our ingestion endpoint (ingestion.inspector.dev) with a new implementation in pure NodeJs. This endpoint receives monitoring data sent from all applications connected to our Code Execution Monitoring engine, and it treats more than 5 million http

Twilio SMS Notification Channel

Hi, I’m Valerio software engineer from Italy, and CTO at Inspector. I am really happy to announce the general availability of our brand new “Twilio – SMS” notification channel. If you are a busy developer you now have one more option to intelligently forward your application monitoring notificiations directly in your smartphone, stay informed about