2016-12-17 2 views
1

async.parallel 함수가 호출하는 콜백을 테스트 중입니다. 콜백이 매개 변수를 사용하거나 none을 사용하면 실행 흐름이 달라진 것으로 보입니다. 모든 기능 완료 후 'endFunctions'콜백이 호출됩니다 nodejs loop 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

결과

는 내가 기대했던 것입니다.

지금 나는 익명 함수가 값을 반환하는 콜백합니다 :

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

async.parralel manual를 읽기, 내가 기대 :

  1. 콜백 'endFunctions'한 번 호출 할 모든 익명의 기능이 종료 된 후
  2. 익명 함수가 반환 한 모든 값의 배열을 수신하는 콜백 'endFunctions'.

제발, 누군가 무슨 일이 일어 났고 무엇이 잘못 됐는지 설명 할 수 있습니까?

+0

나는이 질문은 중복 확실하지 않다 :

다음 코드

적절한 결과를 제공합니다. 그건 분명하지 않습니다. 잘 읽으십시오. – Roge

+0

질문은 async.parallel (nodejs) – Roge

답변

2

콜백 서명이 잘못되었습니다. async.parallel의 경우 콜백에는 두 개의 매개 변수 (오류, 결과)가 있어야합니다.

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