2016-12-17 7 views
1

async.parallel関数によって呼び出されるコールバックをテストしています。コールバックがパラメータを使用する場合と実行しない場合は、実行フローが異なるようです。nodejsループasync.parallelコールバック

var async = require("async"); 
 

 
function makeTestFunction(i) { 
 
\t return function(callback) { 
 
\t \t console.log('test Function: '+i); 
 
\t \t return callback(); 
 
\t }; 
 
} 
 

 
function test(callback) { 
 
\t var endFunctions = function(i) { 
 
\t \t console.log('ending: ' + i); 
 
\t \t return callback(); 
 
\t }; 
 
\t var testFunctions = []; 
 
\t for (var i=0; i < 3; i++) { 
 
\t \t console.log('loop: '+i); 
 
\t \t testFunctions.push(makeTestFunction(i)); 
 
\t } 
 
\t return async.parallel(testFunctions, endFunctions); 
 
} 
 

 
test(function() { 
 
\t console.log('--------------- end test 1'); 
 
}); 
 
// loop: 0 
 
// loop: 1 
 
// loop: 2 
 
// test Function: 0 
 
// test Function: 1 
 
// test Function: 2 
 
// ending: null 
 
// --------------- end test 1

結果は、私が期待したものである:すべての機能が終了した後、 'endFunctions' コールバックが呼び出されます。

今私は、匿名関数が値を返すためにコールバックしたい:async.parralel manualを読ん

var async = require("async"); 
 

 
function makeTestFunction(i) { 
 
\t return function(callback) { 
 
\t \t console.log('test Function: '+i); 
 
\t \t return callback(i); 
 
\t }; 
 
} 
 

 
function test(callback) { 
 
\t var endFunctions = function(i) { 
 
\t \t console.log('ending: ' + i); 
 
\t \t return callback(); 
 
\t }; 
 
\t var testFunctions = []; 
 
\t for (var i=0; i < 3; i++) { 
 
\t \t console.log('loop: '+i); 
 
\t \t testFunctions.push(makeTestFunction(i)); 
 
\t } 
 
\t return async.parallel(testFunctions, endFunctions); 
 
} 
 

 
test(function() { 
 
\t console.log('--------------- end test 2'); 
 
}); 
 
// loop: 0 
 
// loop: 1 
 
// loop: 2 
 
// test Function: 0 
 
// test Function: 1 
 
// ending: 1 
 
// --------------- end test 2 
 
// test Function: 2

を、私は期待:

  1. を一度呼び出されるコールバック 'endFunctions'すべての無名関数が終了した後
  2. 匿名関数によって返されるすべての値の配列を受け取るコールバック 'endFunctions'。

誰かが何が起こったのか、何が間違っているのか説明できますか?

+0

次のコードは、適切な結果を与えます。それは明らかではない。慎重にお読みください – Roge

+0

質問はasync.parallel(nodejs)に関するものです。 – Roge

答えて

2

コールバック署名は間違っています。 async.parallelの場合、コールバックには2つのパラメータ(エラー、結果)が必要です。私はこの質問が重複していることを確認していない

var async = require("async"); 
 

 
function makeTestFunction(i) { 
 
\t console.log('make function: '+i); 
 
\t return function(callback) { 
 
\t \t console.log('test Function: '+i); 
 
\t \t return callback(null, i); 
 
\t }; 
 
} 
 

 
function test(callback) { 
 
\t var endFunctions = function(err, result) { 
 
\t \t console.log('ending: ' + result); 
 
\t \t return callback(); 
 
\t }; 
 
\t var testFunctions = []; 
 
\t for (var i=0; i < 3; i++) { 
 
\t \t (function (k) { 
 
\t \t \t testFunctions.push(makeTestFunction(k)); 
 
\t \t })(i); 
 
\t } 
 
\t return async.parallel(testFunctions, endFunctions); 
 
} 
 

 
test(function() { 
 
\t console.log('--------------- end test 2'); 
 
}); 
 
// make function: 0 
 
// make function: 1 
 
// make function: 2 
 
// test Function: 0 
 
// test Function: 1 
 
// test Function: 2 
 
// ending: 0,1,2 
 
// --------------- end test 2

関連する問題