async.map(['file1','file2','file3'], fs.stat, function(err, results){ // results is now an array of stats for each file });
async.series([ function(){ ... }, function(){ ... } ]);series: 串行执行,一个函数数组中的每个函数,每一个函数执行完成之后才能执行下一个函数。
async.parallel([ function(){ ... }, function(){ ... } ], callback);parallel: 并行执行多个函数,每个函数都是立即执行,不需要等待其它函数先执行。
async.waterfall([ function(callback){ callback(null, 'one', 'two'); }, function(arg1, arg2, callback){ // arg1 now equals 'one' and arg2 now equals 'two' callback(null, 'three'); }, function(arg1, callback){ // arg1 now equals 'three' callback(null, 'done'); } ], function (err, result) { // result now equals 'done' });waterfall: 按顺序依次执行一组函数。每个函数产生的值,都将传给下一个。
async.each(['file1','file2','file3'], function( file, callback) {}, function(err){});对同一个集合中的所有元素都执行同一个异步操作。
async.map(['file1','file2','file3'], fs.stat, function(err, results){ // results is now an array of stats for each file });对集合中的每一个元素,执行某个异步操作,得到结果。所有的结果将汇总到最终的callback里。 与each的区别是,each只关心操作不管最后的值,而map关心的最后产生的值。
async.filter(['file1','file2','file3'], fs.exists, function(results){ // results now equals an array of the existing files });使用异步操作对集合中的元素进行筛选。
原文地址:http://blog.csdn.net/xufeng0991/article/details/45667843