Node JS: The Awesome Async.js

In this blog we are going to see different uses of the async library which is mostly used with with nodejs.

The library can downloaded from https://github.com/caolan/async

This main purpose of this library is to allow efficient handling of tasks in parallel, series and other types of flows. This library provides with easy function to control code flow and perform operations efficiently. This is mainly used to control/process long running process like ajax request, socket, etc.

Installation is very easy, similar to all other modules

npm -g install async

Or to use in a website that js file can be download and including as a script tag.

The library is broadly divided into two sections

  1. Collection

  2. Control Flow

Let go through some code examples

Parallel Operation vs Series Operation on a Single Array

Below is a code to process an array in parallel using the async “each” function. The code is simple, it divides the number by 5 and outputs the result and current time. We have introduced a timeout of 1sec to make the process take some time. As can be seen from the output the time is same for each.

When we use “eachSeries” function, i.e process in series the timers are different with a 1sec interval.

We also have “eachLimit” function to combine series and parallel together.

[http://jsfiddle.net/manish_iitg/9afwoaup/embedded/js,result/]

Parallel Array Manipulation

In below code we use function “map”,”mapSeries” and “mapLimit” to change/update an array. This is a very useful to way to safely update an array with code running parallel.

Example use cases could be:

There is list of email address lets say 100 emails address of users of your website. You need to find out which account are active and which inactive. So this would require an ajax request to your database to fetch this result and update the initial array the user account status. If the current operation is done mapLimit (5 at a time), this would be a very fast and thread safe way of updating the array.

[http://jsfiddle.net/manish_iitg/1qw116un/embedded/js,result/]

Filter Array In Parallel and Series

This is also a very useful way to reduce your array or remove invalid entries from an array.

[http://jsfiddle.net/manish_iitg/o5ddza7j/embedded/js,result/]

Sorting An Array

There is another useful function “sortBy” which can be used to sort an array of data, if sorting is based on the result of a time taking operation like ajax request or socket reply

[http://jsfiddle.net/manish_iitg/cda5edq0/embedded/js,result/]

Queue

Async provides a very good task handler “queue” to process tasks in parallel with a specific number of parallel process (called concurrency)

Task can be added later and various events are fired to monitor the queue. Here is a graphical implementation of the same.

[http://jsfiddle.net/manish_iitg/w1vk4gp1/embedded/js,result/]

These were some of the most used and useful functions of async library.

excellence-social-linkdin
excellence-social-facebook
excellence-social-instagram
excellence-social-skype