The wonderful ways of async processing in NodeJS

Errol Fernandes
4 min readFeb 22, 2019

--

nodejs.org

Multithreading is a huge requirement in most of the applications. As the application usage increases the primary concern for the application owner is how do we handle multiple requests simultaneously to reduce the load over the server.

In NodeJS there are multiple ways using which you can achieve asynchronicity.

Callback

Callback is an asynchronous equivalent for a function. A callback function is called at the completion of a given task. For example, a function to read a file may start reading the file and return the control to the execution environment immediately so that the next instruction can be executed. Once file I/O is complete, it will call the callback function while passing the callback function, the content of the file as a parameter. So there is no blocking or wait for File I/O. This makes Node.js highly scalable, as it can process a high number of requests without waiting for any function to return results. This is explained in the image below:-

Callback Code Snippet
Output

As you can see in the output of the code Program endedprinted first. Later the data in the file (file1.txt) was printed.

Callback-hell:- Asynchronous JavaScript, or JavaScript that uses callbacks, is hard to get right. A lot of code ends up looking like this:

When you have lots of callback functions in your code! It gets harder to work with them the more of them you have in your code and it gets particularly bad when you need to do loops, try-catch blocks and things like that. To tackle this problem Promise was introduced.

Promise

The core idea behind promises is that a promise represents the result of an asynchronous operation. A promise is in one of three different states:

  • pending — The initial state of a promise.
  • fulfilled — The state of a promise representing a successful operation.
  • rejected — The state of a promise representing a failed operation.

Once a promise is fulfilled or rejected, it is immutable (i.e. it can never change again).

Promise code snippet

As you can see in the above code snippet the async code of readFile is executed using promises. One of the benefits of using Promises is the Promise Chaining feature.

As you can see in the above example MakeCoffee and EatBreakfast are two async tasks that we need to execute. If we used the callback process it would have led to a callback hell situation but in Promises using the all()function we can wait until both the tasks are resolved. In the below example using the .then()method we can chain multiple promises one after another.

Promise chaining

Promises are great. If you are still using callback then switching to Promise will help you in writing clear and readable code. But still implementing Promise is not that easy and if not done correctly can lead to chaos.

Async/Await

Async/Await is still based on promises but does provide a nice syntax to work with it. It provides you a synchronous-looking code without the wait!

Async/Await Code snippet
Output

As you can see in the above example, we write the promises in separate modular function and we don't call them using any .then() chaning but simply using the await method in front of the function definition. This makes using Promises and calling them in your code easier and better.

Final Verdict

Javascript is great in terms of async and non-blocking code but it's hard to read and write. Therefore there are a million ways in which you can implement any of the above methods depending on your need and on your skill level.ü

This story is published in The Startup, Medium’s largest entrepreneurship publication followed by +428,678 people.

Subscribe to receive our top stories here.

--

--

Errol Fernandes

AWS Certified Architect, Google Cloud Certified Professional Cloud Architect