Monday 22 July 2019

Handling child process of a child process

Hi everyone. I've written a command line application and use child_process.spawn() to use other external processes.I have promisified my spawn calls, so that I can use async and await to sequence various external processes. Inside the promisifying wrapper, I use childProcess.on('exit', ...) to resolve the Promise.Here's what the code looks like:async function asyncChildProcess( command, commandArgs, commandOptions, options = {}, ) { const { onSuccess = () => {}, } = options; return new Promise((resolve) => { const childProcess = spawn(command, commandArgs, commandOptions); childProcess.on('exit', () => { onSuccess(); resolve(); }); }); } In one instance, one of my child processes spawns its own child process and exits. But since I am listening to the exit signal of the child process which I created, my Promise is prematurely resolved and I end up with a mess.What's the recommended way to handle child processes that spawn their own children, so that I can wait for all descendants of the child process spawned in my code to exit before I resolve the wrapping Promise?

Submitted July 22, 2019 at 01:42PM by nohablocoffeescript

No comments:

Post a Comment