Node.js/exec:修订间差异
来自康健生活
| 第35行: | 第35行: | ||
const result = await exec(cmd).catch(err => console.log(err)); | const result = await exec(cmd).catch(err => console.log(err)); | ||
</syntaxhighlight>}} | </syntaxhighlight>}} | ||
=== 包装方式 === | |||
或者另一种包装方式。 | |||
<syntaxhighlight lang="javascript"> | |||
function exec(cmd, option = {}) { | |||
return new Promise((resolve, reject) => { | |||
child_process.exec(cmd, option, (err, stdout, stderr) => { | |||
if(err) { | |||
return reject(err); | |||
} | |||
resolve(stdout||stderr); | |||
}); | |||
}); | |||
}</syntaxhighlight> | |||