| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 795f32b commit a25d76c
1 file changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -29,6 +29,7 @@ ls.on('close', (code) => { | |||
| 29 | 29 | ||
| 30 | 30 | ```mjs | |
| 31 | 31 | import { spawn } from 'node:child_process'; | |
| 32 | + import { once } from 'node:events'; | ||
| 32 | 33 | const ls = spawn('ls', ['-lh', '/usr']); | |
| 33 | 34 | ||
| 34 | 35 | ls.stdout.on('data', (data) => { | |
@@ -39,9 +40,8 @@ ls.stderr.on('data', (data) => { | |||
| 39 | 40 | console.error(`stderr: ${data}`); | |
| 40 | 41 | }); | |
| 41 | 42 | ||
| 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}`); | ||
| 45 | 45 | ``` | |
| 46 | 46 | ||
| 47 | 47 | By default, pipes for `stdin`, `stdout`, and `stderr` are established between | |
@@ -772,6 +772,7 @@ ls.on('close', (code) => { | |||
| 772 | 772 | ||
| 773 | 773 | ```mjs | |
| 774 | 774 | import { spawn } from 'node:child_process'; | |
| 775 | + import { once } from 'node:events'; | ||
| 775 | 776 | const ls = spawn('ls', ['-lh', '/usr']); | |
| 776 | 777 | ||
| 777 | 778 | ls.stdout.on('data', (data) => { | |
@@ -782,9 +783,8 @@ ls.stderr.on('data', (data) => { | |||
| 782 | 783 | console.error(`stderr: ${data}`); | |
| 783 | 784 | }); | |
| 784 | 785 | ||
| 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}`); | ||
| 788 | 788 | ``` | |
| 789 | 789 | ||
| 790 | 790 | Example: A very elaborate way to run `ps ax | grep ssh` | |
@@ -1483,6 +1483,7 @@ ls.on('exit', (code) => { | |||
| 1483 | 1483 | ||
| 1484 | 1484 | ```mjs | |
| 1485 | 1485 | import { spawn } from 'node:child_process'; | |
| 1486 | + import { once } from 'node:events'; | ||
| 1486 | 1487 | const ls = spawn('ls', ['-lh', '/usr']); | |
| 1487 | 1488 | ||
| 1488 | 1489 | ls.stdout.on('data', (data) => { | |
@@ -1496,6 +1497,9 @@ ls.on('close', (code) => { | |||
| 1496 | 1497 | ls.on('exit', (code) => { | |
| 1497 | 1498 | console.log(`child process exited with code ${code}`); | |
| 1498 | 1499 | }); | |
| 1500 | + | ||
| 1501 | + const [code] = await once(ls, 'close'); | ||
| 1502 | + console.log(`child process close all stdio with code ${code}`); | ||
| 1499 | 1503 | ``` | |
| 1500 | 1504 | ||
| 1501 | 1505 | ### Event: `'disconnect'` | |
| Back | FazBrowse Home | New Git URL |
0 commit comments