Node.js | Let’s talk asynchronous

satya prakash
3 min readJun 16, 2021

--

Hey there, so as you must have read the topic of the blog and understood by now what are we going to talk about in this blog. YES, it will be node.js.

But, before starting to talk about node.js, I want to first talk about asynchronousity, because that is the crux of node.js.
So, consider this simple program to be written in a programming language such as Java, C++, or any other synchronous programming language.

void main() {
connectDB(args);
string res1 = apiCall1(arg1);
manipulate(res1);
string res2 = apiCall2(arg2);
manipulate(res2);
}

As you can interpret that this code will first wait to connect to DB then wait for apiCall1 to return and then move on to apiCall2, so we notice here that most of the time is being wasted on waiting for the functions to return the result first.

But do we actually need to wait for the above functions to complete in order to move ahead with other apicalls? The answer here is NO, we don’t need to wait, but due to the nature of the synchronous behavior of the language, we need to wait. To overcome this we can also use the concept of multi-threading for such languages but there is always a limit to how many threads one can spawn for a program.
At this point, an asynchronous, single-threaded language such as JavaScript will help us out.

function main() {
connectDB(args, (err, res) => {
//...code to execute after DB connect
});
apicall1(arg1, (err, res1) => {
manipulate(res1);
})
apicall2(arg2, (err, res2) => {
manipulate(res2);
})
}

So, while executing this piece of code by a JS engine it will not wait for the above functions to execute and will simply remember it and move to the next function, and whenever the previous function calls are executed and they want to return. The returned value is pushed in the call stack to be used by the function defined as the second argument a.k.a callback function.

I understand that writing such type of callback functions can create multiple callbacks one inside another, this is known as callback hell. To overcome this, we have some concepts in JS such as promises, and the most useful one that I think is async/await. Let’s keep this discussion for some other blog.
So, since JavaScript is single-threaded it needs to use this asynchronous behavior in order to complete multiple tasks and not be stuck on one line and wait for it to execute completely.

What is Node.js then??

Node.js® is a JavaScript runtime built on Chrome’s V8 JavaScript engine.
Earlier before Node.js was introduced, JavaScript was only being used in the browser. With the introduction of Node.js, now JavaScript was extracted out of the browser with the help of Chrome’s V8 engine and now it is able to communicate with the Operating system and now could ask the OS to open network channels, files, HTTP connections, and many more such tasks.
Once it was introduced many frameworks came into the picture to accommodate node.js and allow the backend engineers to write server codes and APIs in an asynchronous way.

One very solid proof for the success of node.js is npm which is the largest package manager for node.js
This helped programmers to reuse the packages written by others and increase their development speed. Today you have packages in npm for almost everything you can imagine.

But, isn’t node.js single-threaded too, and wouldn’t that limit the use of our CPU cores?

Yes, you are correct. To overcome this we do have master and worker threads that can use the other cores. A very good package that you can check out for this specific purpose is PM2, which allows you to run the node.js server on all the possible cores in the CPU and still maintaining the asynchronous behavior. It also has some other awesome features like logging, maintaining error stacktrace, restarting the server in case it goes down for some reason, and many more.

Many companies have been using node.js now for several years, Flipkart being one of them. You might also try checking it out in detail someday.

--

--