Skip to content

Instantly share code, notes, and snippets.

@Deepak13245
Created June 7, 2020 22:34
Show Gist options
  • Save Deepak13245/98e7f71f47a8fd06aa885516846a2c6f to your computer and use it in GitHub Desktop.
Save Deepak13245/98e7f71f47a8fd06aa885516846a2c6f to your computer and use it in GitHub Desktop.
Spawn a subprocess in nodejs
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