2017-08-10 10 views
0

我想触发一个功能,当两个child_process已经完成。我想下面使用的承诺,但它似乎触发Promise.all的承诺得到解决如何知道当两个子进程已解决的NodeJS

let excelParserChildOnePromise = new Promise((resolveChild, rejectChild) => { 
    let excelParserChildOne = fork(excelParserTool); 

    excelParserChildOne.send(`${tempFilePositionOne}`); 
    excelParserChildOne.on('message', (excelArray) => { 
     console.log('child one resolved') 
     resolveChild(excelArray); 
    }) 
}); 

let excelParserChildTwoPromise = new Promise((resolveChild, rejectChild) => { 
    let excelParserChildTwo = fork(excelParserTool); 

    excelParserChildTwo.send(`${tempFilePositionTwo}`); 
    excelParserChildTwo.on('message', (excelArray) => { 
     console.log('child two resolved') 
     resolveChild(excelArray) 
    }) 
}); 


childPromises.push([excelParserChildOnePromise, excelParserChildTwoPromise]); 

Promise.all(childPromises).then(() => { 
    console.log('inside promise all'); 
}) 

此打印出之前,以下

inside promise all 
child one resolved 
child two resolved 

我怎么听当这两个过程完成?

回答

1

你的.push()进入数组是错误的,因为你正在推送一个promise数组,它给你一个数组的数组而不仅仅是一个简单的promise数组,然后Promise.all()获取错误类型的数据(它只是看到一个数组数组),所以它不能正常等待的承诺:

要修复它,改变这一行:

childPromises.push([excelParserChildOnePromise, excelParserChildTwoPromise]); 

这样:

childPromises.push(excelParserChildOnePromise, excelParserChildTwoPromise); 
+0

啊..花了我20分钟反正实现 – forJ

+0

感谢... – forJ

相关问题