Created
April 11, 2022 16:07
-
-
Save pedroricardo/ac32b72ffc58683b0e0f918fc5aba89f to your computer and use it in GitHub Desktop.
Node.js executa o comando no processo filho como exemplo de promessa
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
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