Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@Alexgalinier
Created September 6, 2018 06:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Alexgalinier/4dd2bb1283f6df36c3b01ef5ba4d827c to your computer and use it in GitHub Desktop.
Save Alexgalinier/4dd2bb1283f6df36c3b01ef5ba4d827c to your computer and use it in GitHub Desktop.
spawn with promise
const { spawn } = require('child_process');
const spawnP = async command => {
return new Promise((res, rej) => {
const cmdParts = command.split(' ');
const cmdSpawm = spawn(
cmdParts[0],
cmdParts.length > 1 ? cmdParts.slice(1) : [],
{
stdio: 'inherit'
}
);
cmdSpawm.on('error', data => {
console.error(`Error: ${data}`);
rej();
});
cmdSpawm.on('close', code => {
if (code === 0) {
res();
} else {
rej();
}
});
});
};
module.exports = spawnP;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment