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);
复制代码

注意

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);
复制代码

转载于:https://juejin.im/post/5c3a9c5a51882524a14158d6

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值