async.parallel是流程控制里邊io並行的控制方法,如果async.parallel里邊沒有io操作,那么里邊的函數執行都是串行的。
這里討論一下參數接收的問題,上代碼:
var async = require('async');
function process1(cb){
var tx1 = {address:'dadada'};
var total1 = 1;
return cb(null,tx1,total1);
}
function process2(cb){
var tx2 = {address:"qweweee"};
var total2 = 2;
return cb(null,tx2,total2);
}
async.parallel([
function(done){
process1(done);
},
function(done){
process2(done);
}
],function(err,tx,total){
console.log(tx[0][0])
console.log(tx[0][1]);
console.log(tx[1][0]);
console.log(tx[1][1]);
//console.log(total[0]);
//console.log(total[1]);
});
可以看到process1和process2都有三個參數,我們接收的時候是不是也是用三個參數去接收呢?其實不是,除了第一個err之外,只需要一個接受參數就可以了,如果有多余的,那么他是以數組的形式來展示的,上面代碼的運行結果是:
{ address: 'dadada' }
1
{ address: 'qweweee' }
2
送你幾顆比特幣玩玩:
