2016-10-26 2 views
0

동기식으로 exec를 실행해야하는 상황이 있습니다. 어떻게 할 수 있습니까? 하나의 lib execSync를 찾았습니다. 그러나 그것은 절하된다. 그래서 이것을위한 다른 해결책. 이것에 블루 버드 약속을 사용할 수 있습니까?노드 exec를 동 기적으로 사용하는 방법은 무엇입니까?

for (var i = 0; i < testCasesLength; i++) { 
     var currentTestCase = testCases[i]; 
     output.testCases.push({ passed: false, name: currentTestCase.name, input: currentTestCase.name, output: null }); 
     fs.writeFileSync(path + "/input" + i + ".txt", currentTestCase.input); 
     var command = "cd " + path + " & java Main < input" + i + ".txt"; 
     exec(command, function(error, stdout, stderr) { 
      if (error) { 
       if (exports.stats) { 
        console.log('INFO: '.green + path + '/Main.java contained an error while executing'); 
       } 
       if (error.toString().indexOf('Error: stdout maxBuffer exceeded.') != -1) { 
        output.error = 'Error: stdout maxBuffer exceeded. You might have initialized an infinite loop.'; 
        fn(output); 
       } else { 
        output.error = stderr; 
        fn(output); 
       } 
      } else { 
       if (exports.stats) { 
        console.log('INFO: '.green + path + '/Main.java successfully compiled and executed !'); 
       } 
       // On success test Running 
       if (stdout == currentTestCase.output) { 
        output.testCases[i].passed = true; 
        output.score = output.score + parseInt(currentTestCase.score); 
       } else { 
        output.testCases[i].output = stdout; 
       } 
      } 
     }); 
    } 
    fn(output); 

답변

1

나는 syncronous 자연에서 그걸 얻기 위해 eachSeries 기능 async 모듈을 사용하는 것이 좋습니다

async.eachSeries(testCasesLength,function(item,callback){ 
//return callback after exec command completed, the next iteration will not execute until get callback() 
}); 
관련 문제