Last active
December 14, 2019 16:22
-
-
Save yukin01/9fed805f499503e0c2042b0122872fce to your computer and use it in GitHub Desktop.
Promisify child_process.spawn
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 childProcess = require("child_process"); | |
const spawn = (command, args) => { | |
return new Promise((resolve, reject) => { | |
const p = Array.isArray(args) | |
? childProcess.spawn(command, args, { stdio: "inherit" }) | |
: childProcess.spawn(command, { stdio: "inherit" }); | |
p.on("close", code => resolve(code)); | |
p.on("error", err => reject(err)); | |
}); | |
}; | |
(async () => { | |
await spawn("echo", ["Hello world!"]); | |
await spawn("ls"); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
{ stdio: "inherit" }
をつけると親プロセスの stdio を引き継ぐので$ node spawn.js
としたときにわざわざconsole.log
やconsole.error
を書かなくても stdout と stderr に出力してくれる。