Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit a25d76c

Browse filesBrowse files
dfabulichaduh95
authored andcommitted
doc: recommend events.once to manage 'close' event
`events.once` is a great way to manage the `close` / `error` / `exit` events of a child process. It creates a Promise that is fulfilled when the EventEmitter emits the given event or that is rejected if the EventEmitter emits 'error' while waiting. I think a lot of people wrongly think that managing a spawned child process requires writing boilerplate handlers for `close` and `error`, and that there's no `promisify` solution for spawned child processes. In fact, `events.once` is that solution. The docs should explicitly recommend it in examples. PR-URL: #60017 Reviewed-By: Chemi Atlow <chemi@atlow.co.il>
1 parent 795f32b commit a25d76c
Copy full SHA for a25d76c

File tree

Expand file treeCollapse file tree

1 file changed

+10
-6
lines changed
Open diff view settings
Filter options
Expand file treeCollapse file tree

1 file changed

+10
-6
lines changed
Open diff view settings
Collapse file

‎doc/api/child_process.md‎

Copy file name to clipboardExpand all lines: doc/api/child_process.md
+10-6Lines changed: 10 additions & 6 deletions
  • Display the source diff
  • Display the rich diff
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ ls.on('close', (code) => {
2929

3030
```mjs
3131
import { spawn } from 'node:child_process';
32+
import { once } from 'node:events';
3233
const ls = spawn('ls', ['-lh', '/usr']);
3334

3435
ls.stdout.on('data', (data) => {
@@ -39,9 +40,8 @@ ls.stderr.on('data', (data) => {
3940
console.error(`stderr: ${data}`);
4041
});
4142

42-
ls.on('close', (code) => {
43-
console.log(`child process exited with code ${code}`);
44-
});
43+
const [code] = await once(ls, 'close');
44+
console.log(`child process exited with code ${code}`);
4545
```
4646

4747
By default, pipes for `stdin`, `stdout`, and `stderr` are established between
@@ -772,6 +772,7 @@ ls.on('close', (code) => {
772772
773773
```mjs
774774
import { spawn } from 'node:child_process';
775+
import { once } from 'node:events';
775776
const ls = spawn('ls', ['-lh', '/usr']);
776777

777778
ls.stdout.on('data', (data) => {
@@ -782,9 +783,8 @@ ls.stderr.on('data', (data) => {
782783
console.error(`stderr: ${data}`);
783784
});
784785

785-
ls.on('close', (code) => {
786-
console.log(`child process exited with code ${code}`);
787-
});
786+
const [code] = await once(ls, 'close');
787+
console.log(`child process exited with code ${code}`);
788788
```
789789
790790
Example: A very elaborate way to run `ps ax | grep ssh`
@@ -1483,6 +1483,7 @@ ls.on('exit', (code) => {
14831483
14841484
```mjs
14851485
import { spawn } from 'node:child_process';
1486+
import { once } from 'node:events';
14861487
const ls = spawn('ls', ['-lh', '/usr']);
14871488

14881489
ls.stdout.on('data', (data) => {
@@ -1496,6 +1497,9 @@ ls.on('close', (code) => {
14961497
ls.on('exit', (code) => {
14971498
console.log(`child process exited with code ${code}`);
14981499
});
1500+
1501+
const [code] = await once(ls, 'close');
1502+
console.log(`child process close all stdio with code ${code}`);
14991503
```
15001504
15011505
### Event: `'disconnect'`

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.