@@ -209,8 +209,7 @@ encoding, `Buffer` objects will be passed to the callback instead.
209
209
const { exec } = require (' child_process' );
210
210
exec (' cat *.js missing_file | wc -l' , (error , stdout , stderr ) => {
211
211
if (error) {
212
- console .error (` exec error: ${ error} ` );
213
- return ;
212
+ console .error (` exit(${ error .code } ): ${ error} ` );
214
213
}
215
214
console .log (` stdout: ${ stdout} ` );
216
215
console .error (` stderr: ${ stderr} ` );
@@ -224,6 +223,8 @@ child runs longer than `timeout` milliseconds.
224
223
Unlike the exec(3) POSIX system call, ` child_process.exec() ` does not replace
225
224
the existing process and uses a shell to execute the command.
226
225
226
+ #### exec with promisify
227
+
227
228
If this method is invoked as its [ ` util.promisify() ` ] [ ] ed version, it returns
228
229
a ` Promise ` for an ` Object ` with ` stdout ` and ` stderr ` properties. The returned
229
230
` ChildProcess ` instance is attached to the ` Promise ` as a ` child ` property. In
@@ -306,19 +307,34 @@ can be used to specify the character encoding used to decode the stdout and
306
307
stderr output. If ` encoding ` is ` 'buffer' ` , or an unrecognized character
307
308
encoding, ` Buffer ` objects will be passed to the callback instead.
308
309
310
+ #### execFile with promisify
311
+
309
312
If this method is invoked as its [ ` util.promisify() ` ] [ ] ed version, it returns
310
- a ` Promise ` for an ` Object ` with ` stdout ` and ` stderr ` properties. The returned
311
- ` ChildProcess ` instance is attached to the ` Promise ` as a ` child ` property. In
312
- case of an error (including any error resulting in an exit code other than 0), a
313
- rejected promise is returned, with the same ` error ` object given in the
314
- callback, but with two additional properties ` stdout ` and ` stderr ` .
313
+ a ` Promise ` , with an additional property ` child ` with the ` ChildProcess ` that
314
+ would normally be returned by ` execFile ` .
315
+
316
+ On a successful exit (a zero status code), the promise resolves to an object
317
+ with ` child ` , ` stdout ` , and ` stderr ` properties.
318
+
319
+ On an unsuccessful exit, the promise resolves to the same ` error ` returned in
320
+ the callback, with three additional properties ` child ` , ` stdout ` , ` stderr ` ,
321
+ which provide the output of the process. As with ` exec ` , the exit code is
322
+ attached to the ` code ` property.
315
323
316
324
``` js
317
- const util = require (' util' );
318
- const execFile = util . promisify (require (' child_process' ).execFile );
325
+ const { promisify } = require (' util' );
326
+ const execFile = promisify (require (' child_process' ).execFile );
319
327
async function getVersion () {
320
- const { stdout } = await execFile (' node' , [' --version' ]);
321
- console .log (stdout);
328
+ try {
329
+ const { stdout , stderr , child } = await execFile (' node' , [' --version' ]);
330
+ console .log (` pid ${ child .pid } exit` );
331
+ console .log (` stdout: ${ stdout} ` );
332
+ console .error (` stderr: ${ stderr} ` );
333
+ } catch (error) {
334
+ console .error (` pid ${ error .child .pid } exit(${ error .code } ): ${ error} ` );
335
+ console .log (` stdout: ${ error .stdout } ` );
336
+ console .error (` stderr: ${ error .stderr } ` );
337
+ }
322
338
}
323
339
getVersion ();
324
340
```
0 commit comments