async.series series函数 串行执行它的作用就是按照顺序一次执行。series函数的第一个参数可以是一个数组也可以是一个JSON对象
async.waterfall waterfall和series函数有很多相似之处,都是按照顺序执行。
不同之处是waterfall每个函数产生的值,都将传给下一个函数,而series则没有这个功能
async.parallel parallel函数是并行执行多个函数,每个函数都是立即执行,不需要等待其它函数先执行。
async.parallelLimit parallelLimit函数和parallel类似,但是它多了一个参数limit。
limit参数限制任务只能同时并发一定数量,而不是无限制并发
使用 async.eachLimit 方法实现
async.eachLimit 方法接受四个参数,第一个参数为原始数据数组,第二个参数为每次并行处理的数据量,第三个参数为需要为数据进行的处理,第四个参数为回调函数。使用 async.eachLimit 完成发送邮件任务的思路是定义一个对数据进行处理的函数,然后使用 async.eachLimit 将处理函数应用所有数据上。
let userEmailList = [ '[email protected]', '[email protected]', ..., '[email protected]' ];
let limit = 5;
let processer = function (email) {
sendEmail(email, function (error) {
return callback(error, result);
});
}
async.eachLimit(userEmailList, limit, processer, function (error, result){
console.log(error);
});