Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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