美文网首页
async/mapLimit函数理解

async/mapLimit函数理解

作者: chengfengwang | 来源:发表于2019-01-13 10:02 被阅读0次

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

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函数理解

    官方文档看了很多文章写的都不是很清楚,自己写一下吧。 map map(coll, iteratee, callba...

  • async函数

    async 要理解 async 先要掌握 Promise 的概念,了解 Promise 请戳我! async 函数...

  • 理解用 async/await 来处理异步

    理解用 async/await 来处理异步 async的用法 async 作为一个关键字放到函数前面,用于表示函数...

  • async和await的使用

    async函数 什么是async函数? async函数是使用async关键字声明的函数。 mdn文档:https:...

  • JS中的async/await -- 异步隧道尽头的亮光

    async函数 简单解释async函数就是Generator函数的语法糖。 Generator函数写法 async...

  • nodejs流程控制之async

    本文主要讲解async的函数: series,parallel, waterfall 函数的理解和 使用 asyn...

  • 2018-05-22

    async 函数 1. 含义 async 函数是 Generator 函数的语法糖。async函数将Generat...

  • ES8(13)、await 和 async 的用法

    async函数是使用async关键字声明的函数。 async函数是AsyncFunction构造函数的实例, 并且...

  • async函数

    async函数 async函数的含义 简单来说:async 函数就是 Generator 函数的语法糖下面是两个是...

  • ES8-async&await

    async函数是使用async关键字声明的函数,async函数是AsyncFunction构造函数的实例,返回值是...

网友评论

      本文标题:async/mapLimit函数理解

      本文链接:https://www.haomeiwen.com/subject/ikjkdqtx.html