Skip to content

Commit

Permalink
Improve the readme usage examples (#385)
Browse files Browse the repository at this point in the history
* Cleanup & label examples

* Update readme.md

* Update readme.md
  • Loading branch information
justsml authored and sindresorhus committed Nov 3, 2019
1 parent 4f49253 commit 2956ea1
Showing 1 changed file with 32 additions and 8 deletions.
40 changes: 32 additions & 8 deletions readme.md
Expand Up @@ -41,16 +41,20 @@ const execa = require('execa');
})();
```

Additional examples:
### Pipe the child process stdout to the parent

```js
const execa = require('execa');

(async () => {
// Pipe the child process stdout to the current stdout
execa('echo', ['unicorns']).stdout.pipe(process.stdout);
execa('echo', ['unicorns']).stdout.pipe(process.stdout);
```

### Handling Errors

```js
const execa = require('execa');

(async () => {
// Catching an error
try {
await execa('unknown', ['command']);
Expand All @@ -77,20 +81,33 @@ const execa = require('execa');
*/
}

// Cancelling a spawned process
})();
```

### Cancelling a spawned process

```js
const execa = require('execa');

(async () => {
const subprocess = execa('node');

setTimeout(() => {
subprocess.cancel();
}, 1000);

try {
await subprocess;
} catch (error) {
console.log(subprocess.killed); // true
console.log(error.isCanceled); // true
}
})();
})()
```

### Catching an error with the sync method

// Catching an error with a sync method
```js
try {
execa.sync('unknown', ['command']);
} catch (error) {
Expand All @@ -115,16 +132,23 @@ try {
}
*/
}
```

### Kill a process

Using SIGTERM, and after 2 seconds, kill it with SIGKILL.

// Kill a process with SIGTERM, and after 2 seconds, kill it with SIGKILL
```js
const subprocess = execa('node');

setTimeout(() => {
subprocess.kill('SIGTERM', {
forceKillAfterTimeout: 2000
});
}, 1000);
```


## API

### execa(file, arguments, [options])
Expand Down

0 comments on commit 2956ea1

Please sign in to comment.