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
相關文章
相關標籤/搜索