Created
September 24, 2024 05:41
-
-
Save mingfang/897bffe4213a76b78e0ad3141f5a1329 to your computer and use it in GitHub Desktop.
child_process.spawn as promise
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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