A Promise-based interface into processes created by child_process.spawn


4911.2.85 years ago6 years agoMinified + gzip package size for @exponent/spawn-async in KB


spawn-async CircleCI Build Status

A cross-platform version of Node's child_process.spawn as an async function that returns a promise.


import spawnAsync from '@exponent/spawn-async';

let resultPromise = spawnAsync('echo', ['hello', 'world'], { stdio: 'inherit' });
let spawnedChildProcess = resultPromise.child;
try {
  let {
    output: [stdout, stderr],
  } = await resultPromise;
} catch (e) {
  // The error object also has the same properties as the result object


spawnAsync takes the same arguments as child_process.spawn.

It returns a promise whose result is an object with these properties:

  • pid: the process ID of the spawned child process
  • output: an array with stdout and stderr's output
  • stdout: a string of what the child process wrote to stdout
  • stderr: a string of what the child process wrote to stderr
  • status: the exit code of the child process
  • signal: the signal (ex: SIGTERM) used to stop the child process if it did not exit on its own

If there's an error running the child process or it exits with a non-zero status code, spawnAsync rejects the returned promise. The Error object also has the properties listed above.

If you find any bugs or have a feature request, please open an issue on github!

The npm package download data comes from npm's download counts api and package details come from npms.io.