Nodejs Async Detailed Three: set operation

Source: Internet
Author: User
Tags sorts

Async provides a number of functions for collections that simplify the steps we take when we work with collections asynchronously. As follows:

    1. ForEach: Asynchronous operation for each element in the collection

    2. Map: Get another value for each element in the collection by asynchronous operation, to get a new collection

    3. Filter: Filters The elements in the collection using asynchronous operations to get a set that meets the criteria

    4. Reject: Similar to filter, just the opposite when judging the condition, get the set of the remaining elements

    5. Reduce: Asynchronously operates with an initial value for each element in the collection, and finally gets a unique result

    6. Detect: Gets the first data in the set that satisfies the condition

    7. SortBy: Asynchronously operates on data in a collection and then sorts from small to large by value

    8. Some/any: Whether there is at least one element in the collection that satisfies the condition

    9. Every/all: Whether each element satisfies a condition in the collection

    10. Concat: Asynchronously operates on elements in a collection, merging result sets into an array

Here are one by one explanations:

1. ForEach (arr, iterator (item, callback), callback (ERR))

If you want to perform the same asynchronous operation on all elements in the same collection, you can take advantage of the foreach function. Note that the function focuses on the execution process, ignoring data that is generated after the run. If you need a result, you can use the map function.

Depending on how it is performed, foreach provides three versions:

    1. All elements in the collection are executed in parallel

    2. One by one sequential execution

    3. Execute in batches, parallel in the same batch, in order between batches and batches

First look at the parallel execution of the example, it is relatively simple, just print out the contents of the incoming elements:

var arr = [{name: ' Jack ', delay:200},
{name: ' Mike ', delay:100},
{name: ' Freewind ', delay:300}];

Async.foreach (arr, function (item, callback) {
Log (' 1.1 Enter: ' + item.name);
SetTimeout (function () {
Log (' 1.1 handle: ' + item.name);
Callback ();
}, Item.delay);
}, function (err) {
Log (' 1.1 err: ' + err);
});

It will play the following results:

42.244> 1.1 Enter:jack
42.245> 1.1 Enter:mike
42.245> 1.1 Enter:freewind
42.350> 1.1 Handle:mike
42.445> 1.1 Handle:jack
42.554> 1.1 Handle:freewind
42.554> 1.1 err:undefined

The first data is the current time value (in seconds. milliseconds) from which you can see that each asynchronous operation is executed in parallel.

If you want to perform synchronously, you need to use the Foreachseries function, which is exactly the same as foreach, except that it is executed one at a time. This is not an example.

When there are many elements in the collection, you do not want to do all of the parallel operations at once, and you do not want one in order, you can use the Foreachlimit function. It can be set to process a batch of several, in parallel execution of each batch, between batches and batches executed sequentially.

Async.foreachlimit (arr, 2, function (item, callback) {
Log (' 1.5 enter: ' + item.name);
SetTimeout (function () {
Log (' 1.5 handle: ' + item.name);
Callback (null, item.name);
}, Item.delay);
}, function (err) {
Log (' 1.5 err: ' + err);
});

The printing results are as follows:

42.247> 1.5 Enter:jack
42.248> 1.5 Enter:mike
42.351> 1.5 Handle:mike
42.352> 1.5 Enter:freewind
42.461> 1.5 Handle:jack
42.664> 1.5 Handle:freewind
42.664> 1.5 err:undefined

You can see that the first two start at the same time, and the third one is to wait until the first two are complete.

More detailed examples: https://github.com/freewind/async_demo/blob/master/forEach.js

2. Map (arr, iterator (item, callback), callback (err, results))

The focus of the map is to convert the elements in the collection to another object through an asynchronous operation, and finally to get an array of the transformed objects. It also provides two ways of parallel and sequential execution.

Here is an example that adds an asynchronous way to each element in the collection!!! :

var arr = [{name: ' Jack ', delay:200}, {name: ' Mike ', delay:100}, {name: ' Freewind ', delay:300}, {name: ' Test ', delay:50}];

Async.map (arr, function (item, callback) {
Log (' 1.1 Enter: ' + item.name);
SetTimeout (function () {
Log (' 1.1 handle: ' + item.name);
Callback (NULL, item.name+ '!!! ');
}, Item.delay);
}, function (Err,results) {
Log (' 1.1 err: ', err);
Log (' 1.1 results: ', results);
});

The printing results are as follows:

54.569> 1.1 Enter:jack
54.569> 1.1 Enter:mike
54.569> 1.1 Enter:freewind
54.569> 1.1 Enter:test
54.629> 1.1 Handle:test
54.679> 1.1 Handle:mike
54.789> 1.1 Handle:jack
54.879> 1.1 Handle:freewind
54.879> 1.1 ERR:
54.879> 1.1 Results: [' Jack!!! ', ' Mike!!! ', ' freewind!!! ', ' Test!!! ']

As you can see, the operations on each element are parallel, and the results are aggregated together to the final callback.

If you want to do this sequentially, you can use Mapseries, which is exactly the same as map usage.

More detailed examples: https://github.com/freewind/async_demo/blob/master/map.js

3. Filter (arr, iterator (item, callback (test)), callback (results))

Uses an asynchronous operation to filter the elements in the collection. It is important to note that the iterator callback has only one parameter and can only receive true or FALSE.

For an error, the function does not make any processing and is thrown directly by Nodejs. Therefore, attention should be paid to error handling.

Provides two ways of parallel and sequential execution.

Parallel example, find all elements of >=3:

Async.filter ([1,2,3,4,5], function (item, callback) {
Log (' 1.1 Enter: ' + Item);
SetTimeout (function () {
Log (' 1.1 Test: ' + Item);
Callback (ITEM>=3);
}, 200);
}, function (results) {
Log (' 1.1 results: ', results);
});

The printing results are as follows:

16.739> 1.1 enter:1
16.749> 1.1 Enter:2
16.749> 1.1 Enter:3
16.749> 1.1 Enter:4
16.749> 1.1 Enter:5
16.749> 1.3 enter:1
16.949> 1.1 test:1
16.949> 1.1 Test:2
16.949> 1.1 Test:3
16.949> 1.1 Test:4
16.949> 1.1 Test:5
16.949> 1.1 Results: [3, 4, 5]

All elements that satisfy the condition are found.

If you need to execute sequentially, you can use the Filterseries function, which uses the same as filter.

More detailed examples: https://github.com/freewind/async_demo/blob/master/filter_reject.js

4. Reject (arr, iterator (item, callback (test)), callback (results))

Reject is similar to filter, but behaves just the opposite. When the condition is true, it discards the corresponding element. It also provides two ways of parallel and sequential execution.

Parallel example, remove all >=3 elements:

Async.reject ([1,2,3,4,5], function (item, callback) {
Log (' 1.4 Enter: ' + Item);
SetTimeout (function () {
Log (' 1.4 Test: ' + Item);
Callback (ITEM>=3);
}, 200);
}, function (results) {
Log (' 1.4 results: ', results);
});

The printing results are as follows:

31.359> 1.4 Enter:1
31.359> 1.4 Enter:2
31.359> 1.4 Enter:3
31.359> 1.4 Enter:4
31.359> 1.4 Enter:5
31.559> 1.4 Test:1
31.559> 1.4 Test:2
31.559> 1.4 Test:3
31.559> 1.4 Test:4
31.559> 1.4 Test:5
31.569> 1.4 Results: [1, 2]

If you want to execute sequentially, you can use Rejectseries, which is the same as the reject usage.

More detailed examples: https://github.com/freewind/async_demo/blob/master/filter_reject.js

5. Reduce (arr, memo, iterator (Memo,item,callback), Callback (Err,result))

Reduce allows us to give an initial value, use it with each element in the set, and finally get a value. Reduce moves from left to right through elements, and if you want to right-to-left, you can use Reduceright.

Here is an example that calculates the sum of 100 and all the numbers in a set:

var arr = [1,3,5];

Async.reduce (arr, Max, function (memo, item, callback) {
Log (' 1.1 Enter: ' + Memo + ', ' + item ');
SetTimeout (function () {
Callback (null, Memo+item);
}, 100);
},function (err, result) {
Log (' 1.1 err: ', err);
Log (' 1.1 Result: ', result);
});

Will print out the results:

28.789> 1.1 enter:100, 1
28.889> 1.1 enter:101, 3
28.999> 1.1 enter:104, 5
29.109> 1.1 ERR:
29.109> 1.1 result:109

It is important to note that the reduce in async is not a parallel operation, but rather a sequential operation of elements, so when the elements are relatively long, performance is weaker. If you want to improve performance, you can use the Async.map function to get the value after each element in the collection is processed in parallel, and then use the Array.prototype.reduce function to handle it much faster.

For this example:

Async.reduce (arr, max, function (memo,item,callback) {
Log (' 1.4 Enter: ' +memo+ ', ' +item ');
T.inc (item, function (Err,n) {
Log (' 1.4 Handle: ', n);
Callback (null, memo+n);
});
}, function (Err,result) {
Log (' 1.4 err: ', err);
Log (' 1.4 Result: ', result);
});

It takes 0.62 seconds to complete. If you switch to Map+array.reduce:

Async.map (arr, function (item, callback) {
Log (' 1.5 Enter: ', item);
T.inc (item, function (Err,n) {
Log (' 1.5 handle: ', n);
Callback (NULL,N);
});
},function (err, results) {
Log (' 1.5 err: ', err);
Log (' 1.5 results: ', results);
var sum = results.reduce (function (memo, item) {
return memo + item;
}, 100);
Log (' 1.5 sum: ', sum);
});

Time is 0.21 seconds.

More detailed examples: https://github.com/freewind/async_demo/blob/master/reduce.js

6. Detect (array, iterator (Item,callback (test)), callback (Result)

Used to get the first element in the collection that satisfies a condition. It is divided into two ways: parallel and sequential, corresponding to the function detect and detectseries respectively.

Parallel example, find an odd number:

var arr = [{value:1,delay:500},
{value:2,delay:200},
{value:3,delay:300}];
Async.detect (arr, function (item,callback) {
Log (' 1.1 Enter: ', item.value);
SetTimeout (function () {

Log (' 1.1 handle: ', item.value);
Callback (N%2===1);
}, Item.delay);
}, function (Result) {
Log (' 1.1 Result: ', result);
});

The results are as follows:

09.928> 1.1 enter:1
09.928> 1.1 Enter:2
09.928> 1.1 Enter:3
10.138> 1.1 Handle:2
10.228> 1.1 Handle:3
10.228> 1.1 Result: {value:3, delay:300}
10.438> 1.1 handle:1
10.438> 1.1 handle:1

Can be seen the first execution of the odd 3.

More detailed examples: https://github.com/freewind/async_demo/blob/master/detect.js

7. SortBy (Array, iterator (Item,callback (Err,result)), Callback (Err,results))

Sorts the elements in the collection, starting from small to large, based on the values produced by each element after an asynchronous operation.

Example:

var arr = [3,6,1];

Async.sortby (arr, function (item, callback) {
SetTimeout (function () {
Callback (Null,item);
}, 200);
}, function (Err,results) {
Log (' 1.1 err: ', err);
Log (' 1.1 results: ', results);
});

The printing results are as follows:

26.562> 1.1 Err:null
26.562> 1.1 Results: [1, 3, 6]

You can see that the data in the collection is sorted from small to large.

More detailed examples: https://github.com/freewind/async_demo/blob/master/sortBy.js

8. Some/any (arr, iterator (Item,callback (test)), callback (result))

If there is at least one element in the collection that satisfies the condition, the final callback gets the value true, otherwise false. It has a name of No.

Determines whether an element in the collection is less than or equal to 3:

Async.some ([1,2,3,6], function (item,callback) {
Log (' 1.1 Enter: ', item);
SetTimeout (function () {
Log (' 1.1 handle: ', item);
Callback (ITEM<=3);
},100);
}, function (Result) {
Log (' 1.1 Result: ', result);
});

The printing results are as follows:

36.165> 1.1 enter:1
36.165> 1.1 Enter:2
36.165> 1.1 Enter:3
36.165> 1.1 Enter:6
36.275> 1.1 handle:1
36.275> 1.1 Result:true
36.275> 1.1 Handle:2
36.275> 1.1 Handle:3
36.275> 1.1 Handle:6

It is visible that the result is true.

More detailed examples: https://github.com/freewind/async_demo/blob/master/some.js

9. Every/all (arr, iterator (Item,callback), callback (result))

If each element in the collection satisfies the condition, the result passed to the final callback is true, otherwise false

In the following example, the final result is true because each element in the collection is <=10

Async.every (arr, function (item,callback) {
Log (' 1.1 Enter: ', item);
SetTimeout (function () {
Log (' 1.1 handle: ', item);
Callback (ITEM<=10);
},100);
}, function (Result) {
Log (' 1.1 Result: ', result);
});

Print as follows:

32.113> 1.1 enter:1
32.123> 1.1 Enter:2
32.123> 1.1 Enter:3
32.123> 1.1 Enter:6
32.233> 1.1 handle:1
32.233> 1.1 Handle:2
32.233> 1.1 Handle:3
32.233> 1.1 Handle:6
32.233> 1.1 Result:true

Visible end result is true

More detailed examples: https://github.com/freewind/async_demo/blob/master/every.js

Concat (arr, iterator (Item,callback (Err,result)), Callback (Err,result))

Merges the results of multiple asynchronous operations into an array.

In the following example, each element in the collection is doubled:

Async.concat ([' AA ', ' BB '], function (item,callback) {

SetTimeout (function () {

Callback (null, [item, item]);

}, 100);

}, function (err, values) {

Log (' 1.1 err: ', err);

Log (' 1.1 values: ', values);

});

Print as follows:

13.539> 1.1 ERR:

13.639> 1.1 values: [' AA ', ' AA ', ' BB ', ' BB ']

The merged array is printed.

More detailed examples: https://github.com/freewind/async_demo/blob/master/concat.js

Nodejs Async Detailed Three: set operation

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.