Created
June 7, 2020 22:34
-
-
Save Deepak13245/98e7f71f47a8fd06aa885516846a2c6f to your computer and use it in GitHub Desktop.
Spawn a subprocess in nodejs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const { spawn } = require('child_process'); | |
async function spawnChild(command) { | |
return new Promise((resolve, reject) => { | |
const proc = spawn(command, [], { | |
shell: true, | |
}); | |
proc.stdout.pipe(process.stdout); | |
proc.stderr.pipe(process.stderr); | |
proc.on('close', (code) => { | |
console.info(`child process exited with code ${code}`); | |
if (code === 0) { | |
resolve('ok'); | |
} else { | |
reject(new Error('Something went wrong')); | |
} | |
}); | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment