async/mapLimit函数理解

官方文档 看了不少文章写的都不是很清楚,本身写一下吧。html

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.git

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.github

使用方法:

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) )promise

note的翻译参见文章最后bash

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是个异步函数app

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以后,咱们发现好像全部的异步都同时发生了,若是咱们不须要同时执行这么多异步,就能够使用mapLimitdom

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的顺序一致async

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
相关文章
相关标签/搜索