Web Programming TutorialsHow To Handle Async Operations in Parallel in Nodejs

How To Handle Async Operations in Parallel in Nodejs

JavaScript code we write gets executed on a single thread. Nodejs is a runtime platform that uses JavaScript in a single-threaded environment on the server side. In Nodejs, functions are executed asynchronously because of the single threaded nature. This allows multiple clients to be served simultaneously, without blocking the Input/Output queue of operations.

To write and execute these asynchronous operations, there are many suitable ways in NodeJS. If you remember, when you first learn about Nodejs, you learned about callbacks as they are one of the ways to handle asynchronous operations. Next, with the improvement of JavaScript as a programming language, it is common to use promises or async/await syntax nowadays to handle the same asynchronous operations.

In the meantime, you can also read about Best Node.js Coding Practices for Beginners.

Below, you are going to learn about different techniques on how to use Parallel execution of asynchronous operations in NodeJS.

Using Third Party Library

One of the easiest and earliest ways to handle the running of asynchronous operations in parallel and avoid getting into the problem of callback-hell, npm package async.js is quite popular. You can install this package into any NodeJS project by running the following command.

npm install async --save

Async.js helps to structure your applications and makes the control flow of the app easier to understand. For example, if there are multiple text files available to you and you want to execute separate asynchronous operations to each one of them in parallel (if there result of one file does not depend on the other, this scenario is possible), you will take a similar approach as below.

async.parallel(['file1', 'file2', 'file3'], fs.stat, function(err, results) {
    // results is now an array of stats for each file
});

The biggest disadvantage of this approach is that it is a third party library and can go un-maintained any day. It is okay to use a third-party library for hefty tasks but if you concentrate on learning more about the ways of working with default syntax made available by the programming language, it is better to use the native syntax.

The Promise Way

Promises were the first native JavaScript syntax introduced to avoid callback hell. If you do not know what a promise is yet, well it can be defined as following.

The Promise object is used for deferred and asynchronous computations. A Promise represents an operation that hasn’t completed yet but is expected in the future.

The promise is nothing new to the programming world. It is a concept of the 1970s and is also known as channels in some languages. In modern-day JavaScript, it has become quite popular.

Promises are implemented in the node.js module promise. This module is not included unless you add it.

When you call a function and expect a return value, and the value is not yet available, the function instead returns a promise. The function calling it, can then store the promise for later or schedule a succeeding operation when it gets completed. Promises have to be seen as a more specialized form the EventEmitter module (in terms of Nodejs), where there are only two events: reject or resolve. Instead of using an event listen similar to one to listen for the events, in promises, you use them.

Promises always return a value. They can easily be used to perform several asynchronous functions in parallel like the following way.

const fs = require('fs');
const Promise = require('promise');

const readdir_promise = Promise.convertNodeAsyncFunction(fs.readdir);
const readFile_promise = Promise.convertNodeAsyncFunction(fs.readFile);

p = readdir_promise('.');
p.then(
    function(files) {
        // Create an array of promises
        let promises = [];

        for (let i = 0; i < files.length; i++) {
            promises.push(readFile_promise(files[i]));
        }

        Promise.all(promises).then(
            function(results) {
                let totalBytes = 0;
                for (i = 0; i < results.length; i++) {
                    totalBytes += results[i].length;
                }
                console.log('Done reading files. totalBytes = ' + totalBytes);
            },
            function(error) {
                console.log('Error reading files');
            }
        );
    },
    function(error) {
        console.log('readdir failed.');
    }
);

Using async/await

async/await approach is the latest syntax in JavaScript. There are many advantages of using this syntax over normal promises. One of them being is that code readability improves over time since this syntax makes the code shorter when implementing the same functionality in your NodeJS application as it could be done with a promise.

Since its official inception in ES8, async/await has become a centerpiece for the future of JavaScript. Every day new NPM packages getting converted to use this syntax.

There is no particular syntax to parallel execution with async/await but we can utilize Promise.all against an array (or any iterable) of promises to get the expected results like below.

const [user1, user2] = await Promise.all([db.get('user1'), db.get('user2')]);

Promises.all combine a list of all promises used, into a single promise. This then will return all values resolved by those promises in an array when the promises have all been resolved. This happens in parallel and is simple to write and execute.

async/await is powered by years of practice and expertise using nearly perfected promises and syntax as sweet as it gets while maintaining much of the core asynchronous concepts that NodeJS has popularized. Another advantage hue of using this approach is that you are not relying on installing a third party library. This certainly means that you can use this syntax in other JavaScript frameworks such as Angular and React.

To get deeper insights into the Nodejs you can opt for the Elite Nodejs Course helping you to become professional in just 3 weeks.

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Exclusive content

- Advertisement -

Latest article

21,501FansLike
4,106FollowersFollow
106,000SubscribersSubscribe

More article

- Advertisement -