@@ -41,16 +41,20 @@ const execa = require('execa');
41
41
})();
42
42
```
43
43
44
- Additional examples:
44
+ ### Pipe the child process stdout to the parent
45
45
46
46
``` js
47
47
const execa = require (' execa' );
48
48
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
+ ```
52
51
52
+ ### Handling Errors
53
53
54
+ ``` js
55
+ const execa = require (' execa' );
56
+
57
+ (async () => {
54
58
// Catching an error
55
59
try {
56
60
await execa (' unknown' , [' command' ]);
@@ -77,20 +81,33 @@ const execa = require('execa');
77
81
*/
78
82
}
79
83
80
- // Cancelling a spawned process
84
+ })();
85
+ ```
86
+
87
+ ### Cancelling a spawned process
88
+
89
+ ``` js
90
+ const execa = require (' execa' );
91
+
92
+ (async () => {
81
93
const subprocess = execa (' node' );
94
+
82
95
setTimeout (() => {
83
96
subprocess .cancel ();
84
97
}, 1000 );
98
+
85
99
try {
86
100
await subprocess;
87
101
} catch (error) {
88
102
console .log (subprocess .killed ); // true
89
103
console .log (error .isCanceled ); // true
90
104
}
91
- })();
105
+ })()
106
+ ```
107
+
108
+ ### Catching an error with the sync method
92
109
93
- // Catching an error with a sync method
110
+ ``` js
94
111
try {
95
112
execa .sync (' unknown' , [' command' ]);
96
113
} catch (error) {
@@ -115,16 +132,23 @@ try {
115
132
}
116
133
*/
117
134
}
135
+ ```
136
+
137
+ ### Kill a process
138
+
139
+ Using SIGTERM, and after 2 seconds, kill it with SIGKILL.
118
140
119
- // Kill a process with SIGTERM, and after 2 seconds, kill it with SIGKILL
141
+ ``` js
120
142
const subprocess = execa (' node' );
143
+
121
144
setTimeout (() => {
122
145
subprocess .kill (' SIGTERM' , {
123
146
forceKillAfterTimeout: 2000
124
147
});
125
148
}, 1000 );
126
149
```
127
150
151
+
128
152
## API
129
153
130
154
### execa(file, arguments, [ options] )
0 commit comments