Skip to content

Commit 2956ea1

Browse files
justsmlsindresorhus
authored andcommitted
Improve the readme usage examples (#385)
* Cleanup & label examples * Update readme.md * Update readme.md
1 parent 4f49253 commit 2956ea1

File tree

1 file changed

+32
-8
lines changed

1 file changed

+32
-8
lines changed

readme.md

Lines changed: 32 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -41,16 +41,20 @@ const execa = require('execa');
4141
})();
4242
```
4343

44-
Additional examples:
44+
### Pipe the child process stdout to the parent
4545

4646
```js
4747
const execa = require('execa');
4848

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

52+
### Handling Errors
5353

54+
```js
55+
const execa = require('execa');
56+
57+
(async () => {
5458
// Catching an error
5559
try {
5660
await execa('unknown', ['command']);
@@ -77,20 +81,33 @@ const execa = require('execa');
7781
*/
7882
}
7983

80-
// Cancelling a spawned process
84+
})();
85+
```
86+
87+
### Cancelling a spawned process
88+
89+
```js
90+
const execa = require('execa');
91+
92+
(async () => {
8193
const subprocess = execa('node');
94+
8295
setTimeout(() => {
8396
subprocess.cancel();
8497
}, 1000);
98+
8599
try {
86100
await subprocess;
87101
} catch (error) {
88102
console.log(subprocess.killed); // true
89103
console.log(error.isCanceled); // true
90104
}
91-
})();
105+
})()
106+
```
107+
108+
### Catching an error with the sync method
92109

93-
// Catching an error with a sync method
110+
```js
94111
try {
95112
execa.sync('unknown', ['command']);
96113
} catch (error) {
@@ -115,16 +132,23 @@ try {
115132
}
116133
*/
117134
}
135+
```
136+
137+
### Kill a process
138+
139+
Using SIGTERM, and after 2 seconds, kill it with SIGKILL.
118140

119-
// Kill a process with SIGTERM, and after 2 seconds, kill it with SIGKILL
141+
```js
120142
const subprocess = execa('node');
143+
121144
setTimeout(() => {
122145
subprocess.kill('SIGTERM', {
123146
forceKillAfterTimeout: 2000
124147
});
125148
}, 1000);
126149
```
127150

151+
128152
## API
129153

130154
### execa(file, arguments, [options])

0 commit comments

Comments
 (0)