暂停循环直到子进程在Node.js中发送消息

时间:2019-04-01 05:23:06

标签: node.js

我有一个进程,它将消息传递到子进程中,以便在循环的中间进行处理,然后应根据子进程的响应继续进行操作。

但是我不知道如何暂停循环以使子进程完成并在收到消息后继续进行。

以下是该函数的外观

function runTests()
{
  allTests = getTests()
  allTests.forEach(test => {
    console.log(chalk.blue.bold.underline('Running: '+ test.title))
    for(i = 0;i < test.questions.length;i++)
    {
      //Fork child process. 
      const poll = fork('poll.js',['999999', new Date()])
      poll.send(test.questions[i])
      poll.on('message', msg => {

     if(assert("msg.includes('error')",msg))
     {
      console.log(chalk.blue.bold.underline('Child response: '+ msg))
     }
  });
 }
}) 
}

0 个答案:

没有答案