Skip to content

Instantly share code, notes, and snippets.

@mingfang
Created September 24, 2024 05:41
Show Gist options
  • Save mingfang/897bffe4213a76b78e0ad3141f5a1329 to your computer and use it in GitHub Desktop.
Save mingfang/897bffe4213a76b78e0ad3141f5a1329 to your computer and use it in GitHub Desktop.
child_process.spawn as promise
import child_process from 'child_process';
function spawnPromise(cmd, args, options) {
return new Promise(function (resolve, reject) {
const process = child_process.spawn(cmd, args, options);
process.on('close', function (code) {
resolve(code);
});
process.on('error', function (err) {
reject(err);
});
});
}
(async function main(){
try{
const code = await spawnPromise('ls', [], {
stdio: 'inherit'
})
process.exit(code);
}catch(err){
console.log('error', err.message)
}
})()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment