JSPM

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

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

Package Exports

  • @expo/spawn-async

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 (@expo/spawn-async) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

spawn-async CircleCI Build Status

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

Usage:

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

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

API

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.

Accessing the child process

Sometimes you may want to access the child process object--for example, if you wanted to attach event handlers to stdio or stderr and process data as it is available instead of waiting for the process to be resolved.

You can do this by accessing .child on the Promise that is returned by spawnAsync.

Here is an example:

(async () => {
    let ffmpeg$ = spawnAsync('ffmpeg', ['-i', 'path/to/source.flac', '-codec:a', 'libmp3lame', '-b:a', '320k', '-ar', '44100', 'path/to/output.mp3']);
    let childProcess = ffmpeg$.child;
    childProcess.stdout.on('data', (data) => {
      console.log(`ffmpeg stdout: ${data}`);
    });
    childProcess.stderr.on('data', (data) => {
      console.error(`ffmpeg stderr: ${data}`);
    });
    let result = await ffmpeg$;
    console.log(`ffmpeg pid ${result.pid} exited with code ${result.code}`);
})();