node spawn but return promise.
npm i spawnp --save
let spawnp = require('spawnp');
spawnp('echo', ['123']).then(() => {
// finshed
}).catch(() => {
// erorred
});
spawnp(command[, args][, options][, extra])
- command
Just like spawn's command, but support arg in command, like echo 123
spawnp('echo 123', ['456']); // 123 456\n
- args
Just like spawn's args
- options
Just like spawn's options
- extra
Object, {onChild, stdout, stderr}, see next.
spawnp('echo', ['123'], null, {
onChild: (child) => {
child.stdout.on('data', (chunk) => {
console.log(chunk.toString()); //123\n
});
};
}); // return a promise
spawnp('echo', ['123'], null, {
stdout: true // config stdout option
}).then(({
stdouts // then will get stdout chunks
}) => {
console.log(stdouts.join('')); //'123\n';
});
spawnp('ls', ['oooooooooo'], null, {
stderr: true
}).catch(({
stderrs
}) => {
console.log(stderrs.join(''));
});