Package Exports
- promisify-child-process
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (promisify-child-process) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
promisify-child-process
seriously like the best async child process library
Based upon child-process-async
,
but more thorough, because that package doesn't seem very actively maintained.
promisify-child-process
provides a drop-in replacement for the
original child_process
functions, not just duplicate methods that
return a Promise
. So when you call exec(...)
we still return a
ChildProcess
instance, just with .then()
and .catch()
added to
make it promise-friendly.
Install
npm install --save promisify-child-process
Usage
// OLD:
const { exec, spawn, fork, execFile } = require('child_process');
// NEW:
const { exec, spawn, fork, execFile } = require('promisify-child-process');
If for any reason you need to wrap a ChildProcess
you didn't create,
you can use the exported promisifyChildProcess
function:
const {promisifyChildProcess} = require('promisify-child-process');
async function() {
const { stdout, stderr } = await promisifyChildProcess(
some3rdPartyFunctionThatReturnsChildProcess()
)
}
Examples
exec()
async function() {
const { stdout, stderr } = await exec('ls -al');
// OR:
const child = await exec('ls -al', {});
// do whatever you want with `child` here - it's a ChildProcess instance just
// with promise-friendly `.then()` & `.catch()` functions added to it!
child.stdin.write(...);
child.stdout.pipe(...);
child.stderr.on('data', (data) => ...);
const { stdout, stderr } = await child;
}
spawn()
async function() {
const { stdout, stderr, exitCode } = await spawn('ls', [ '-al' ]);
// OR:
const child = spawn('ls', [ '-al' ], {});
// do whatever you want with `child` here - it's a ChildProcess instance just
// with promise-friendly `.then()` & `.catch()` functions added to it!
child.stdin.write(...);
child.stdout.pipe(...);
child.stderr.on('data', (data) => ...);
const { stdout, stderr, exitCode } = await child;
}