Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/ralphtheninja/await-spawn

child_process.spawn() wrapped in a Promise
https://github.com/ralphtheninja/await-spawn

async await child-process promise spawn

Last synced: about 1 month ago
JSON representation

child_process.spawn() wrapped in a Promise

Awesome Lists containing this project

README

        

# await-spawn

> `child_process.spawn()` wrapped in a `Promise` for doing async/await.

[![npm](https://img.shields.io/npm/v/await-spawn.svg)](https://www.npmjs.com/package/await-spawn)
![Node version](https://img.shields.io/node/v/await-spawn.svg)
[![Build Status](https://travis-ci.org/ralphtheninja/await-spawn.svg?branch=master)](https://travis-ci.org/ralphtheninja/await-spawn)
[![JavaScript Style Guide](https://img.shields.io/badge/code_style-standard-brightgreen.svg)](https://standardjs.com)

## Install

```
$ npm i await-spawn -S
```

## Usage

```js
const spawn = require('await-spawn')

const main = async () => {
try {
const bl = await spawn('ls', ['-al'])
console.log(bl.toString())
} catch (e) {
console.log(e.stderr.toString())
}
}

main()
```

## Api

Exposes a single function, which has the same api as `child_process.spawn()`.

Returns a `Promise` with `.child` set to the spawned child process. The `Promise` resolves to the buffered output of `child.stdout` in the form of a [`BufferList`] object.

If there was an error, the `Promise` rejects with an `Error` object, which has the following extra properties:

* `code` the error code
* `stdout` the buffered output of `stdout` in the form of a [`BufferList`] object
* `stderr` the buffered output of `stderr` in the form of a [`BufferList`] object

Note that `child.stdout` doesn't exist if `options.stdio === 'inherit'`, so the `Promise` resolves to `''`.

## License

MIT

[`BufferList`]: https://github.com/rvagg/bl