JSPM

  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 151651
  • Score
    100M100P100Q170758F
  • License MIT

seriously like the best async child process library

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

Build Status Coverage Status semantic-release Commitizen friendly npm version

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 and Set-up

npm install --save promisify-child-process

If you are using a old version of Node without build-in Promises or Object.create, you will need to use polyfills.

Upgrading to v3

You must now pass maxBuffer or encoding to spawn/fork if you want to capture stdout or stderr.

Warning: capturing output

exec and execFile capture stdout and stderr by default. But spawn and fork don't capture stdout and stderr unless you pass an encoding or maxBuffer option:

const { spawn } = require('promisify-child-process');

async function() {
  // captures output
  const { stdout, stderr } = await spawn('ls', [ '-al' ], {encoding: 'utf8'});
  const { stdout, stderr } = await spawn('ls', [ '-al' ], {maxBuffer: 200 * 1024});

  // BUG, DOESN'T CAPTURE OUTPUT:
  const { stdout, stderr } = await spawn('ls', [ '-al' ]);
}

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 = 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, code } = await spawn('ls', [ '-al' ], {encoding: 'utf8'});
  // 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, code } = await child;
}