如何使用 async/await Promises 读取 NodeJS child-process.exec 的 stdout/stderr
你想使用 NodeJS child-process.exec 运行类似 file my.pdf 的命令并在完成后获取其 stdout。
解决方案
TL;DR: (await exec('file my.pdf')).stdout
我们在这里使用 child-process-promise 来简化我们的实现。使用 npm i --save child-process-promise 安装它!
exec_stdout_example.js
const { exec } = require('child-process-promise');
async function run () {
const ret = await exec(`file my.pdf`);
return ret.stdout;
}
run().then(console.log).catch(console.error);你也可以使用 .stderr 而不是 .stdout 来获取 stderr 输出作为字符串
Check out similar posts by category:
Javascript, NodeJS
If this post helped you, please consider buying me a coffee or donating via PayPal to support research & publishing of new posts on TechOverflow