async/mapLimit函数理解

官方文档
看了很多文章写的都不是很清楚,自己写一下吧。

map

map(coll, iteratee, callbackopt)

Produces a new collection of values by mapping each value in coll through the iteratee function. The iteratee is called with an item from coll and a callback for when it has finished processing. Each of these callback takes 2 arguments: an error, and the transformed item from coll. If iteratee passes an error to its callback, the main callback (for the map function) is immediately called with the error.

Note, that since this function applies the iteratee to each item in parallel, there is no guarantee that the iteratee functions will complete in order. However, the results array will be in the same order as the original coll.

使用方法:

import map from 'async/map';
map(coll, iteratee, callbackopt)

上面文档翻译一下就是:
map 使用iteratee函数遍历项目(数组、Object)里的所有item,产生一个新的集合。iteratee函数接受两个参数:item,callback。item是coll的每一项,callback又接受两个参数:error 和 transformedItem,数据处理完成后要手动调用callback。错误发生时,callbackopt会立即执行返回一个错误。callbackopt是在所有的遍历完成之后(依赖iteratee函数里面的callback执行)才调用的,它接收两个参数:error和results,err是iteratee遍历函数里产生的error,results是最终的结果数组(类似于 results.push(transformedItem) )

note的翻译参见文章最后

Demo

var mapLimit = require('async/mapLimit');
var map = require('async/map');
var str = 'abcdefgh';
var arr = str.split('');

var iterateeFunction = function(item,callback){
    var newItem = item + 1;
    callback(null,newItem);
};
var allEndFunction = function(err,results){
    console.log(results) //[ 'a1', 'b1', 'c1', 'd1', 'e1', 'f1', 'g1', 'h1' ]
};
map(str,iterateeFunction,allEndFunction);

看起来好像跟数组的Array.prototype.map方法没啥区别,但是这个iteratee是个异步函数

var mapLimit = require('async/mapLimit');
var map = require('async/map');
var str = 'abcdefgh';
var arr = str.split('');

var iterateeFunction = function (item, callback) {
    console.log('调用开始')
    setTimeout(() => {
        var newItem = item + 1;
        console.log('调用结束')
        callback(null, newItem);
    }, Math.random() * 1000);
};
var allEndFunction = function (err, results) { //allEndFunction会在所有异步执行结束后再调用,有点像promise.all
    console.log(results) //[ 'a1', 'b1', 'c1', 'd1', 'e1', 'f1', 'g1', 'h1' ]
};
map(str, iterateeFunction, allEndFunction); //allEndFunction会在所有异步执行结束后再调用,有点像promise.all

跑完demo之后,我们发现好像所有的异步都同时发生了,如果我们不需要同时执行这么多异步,就可以使用mapLimit

mapLimit

mapLimit(coll, limit, iteratee, callbackopt)
只是多了一个limit参数,理解了map,这个也会了

var mapLimit = require('async/mapLimit');
var map = require('async/map');
var str = 'abcdefgh';
var arr = str.split('');

var iterateeFunction = function (item, callback) {
    console.log('调用开始' + item)
    setTimeout(() => {
        var newItem = item + 1;
        console.log('调用结束' + item)
        callback(null, newItem);
    }, Math.random() * 1000);
};
var allEndFunction = function (err, results) {
    console.log(results) //[ 'a1', 'b1', 'c1', 'd1', 'e1', 'f1', 'g1', 'h1' ]
};
mapLimit(str,1, iterateeFunction, allEndFunction);
image.png

注意

Note, that since this function applies the iteratee to each item in parallel, there is no guarantee that the iteratee functions will complete in order. However, the results array will be in the same order as the original coll.
map的iteratee函数都是平行执行,所以不保证iteratee的完成顺序,但是results array的顺序和原coll的顺序一致

var mapLimit = require('async/mapLimit');
var map = require('async/map');
var str = 'abcdefgh';
var arr = str.split('');

var iterateeFunction = function (item, callback) {
    console.log('调用开始' + item)
    setTimeout(() => {
        var newItem = item + 1;
        console.log('调用结束' + item)
        callback(null, newItem);
    }, Math.random() * 1000);
};
var allEndFunction = function (err, results) {
    console.log(results) //[ 'a1', 'b1', 'c1', 'd1', 'e1', 'f1', 'g1', 'h1' ]
};
map(str, iterateeFunction, allEndFunction);
image.png

你可能感兴趣的:(async/mapLimit函数理解)