Skip to content

Instantly share code, notes, and snippets.

@pedroricardo
Created April 11, 2022 16:07
Show Gist options
  • Save pedroricardo/ac32b72ffc58683b0e0f918fc5aba89f to your computer and use it in GitHub Desktop.
Save pedroricardo/ac32b72ffc58683b0e0f918fc5aba89f to your computer and use it in GitHub Desktop.
Node.js executa o comando no processo filho como exemplo de promessa
const { exec } = require('child_process')
function run(cmd) {
return new Promise((resolve, reject) => {
exec(cmd, (error, stdout, stderr) => {
if (error) return reject(error)
if (stderr) return reject(stderr)
resolve(stdout)
})
})
}
// usage example
;(async () => {
const result = await run('echo "hello"')
console.log(result) // hello
})()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment