Skip to content

Commit f418a22

Browse files
vsemozhetbytsam-github
authored andcommitted
doc: modernize child_process example code
1. equal => strictEqual. 2. let => const for the variable that is not reassigned. 3. fix spaces. 4. stringify erroneous raw buffer outputs. 5. fix a typo. PR-URL: #10102 Reviewed-By: Sam Roberts <[email protected]> Reviewed-By: Sakthipriyan Vairamani <[email protected]>
1 parent 10929f6 commit f418a22

File tree

1 file changed

+17
-17
lines changed

1 file changed

+17
-17
lines changed

doc/api/child_process.md

+17-17
Original file line numberDiff line numberDiff line change
@@ -91,11 +91,11 @@ const spawn = require('child_process').spawn;
9191
const bat = spawn('cmd.exe', ['/c', 'my.bat']);
9292

9393
bat.stdout.on('data', (data) => {
94-
console.log(data);
94+
console.log(data.toString());
9595
});
9696

9797
bat.stderr.on('data', (data) => {
98-
console.log(data);
98+
console.log(data.toString());
9999
});
100100

101101
bat.on('exit', (code) => {
@@ -113,7 +113,7 @@ exec('my.bat', (err, stdout, stderr) => {
113113
});
114114

115115
// Script with spaces in the filename:
116-
const bat = spawn('"my script.cmd"', ['a', 'b'], { shell:true });
116+
const bat = spawn('"my script.cmd"', ['a', 'b'], { shell: true });
117117
// or:
118118
exec('"my script.cmd" a b', (err, stdout, stderr) => {
119119
// ...
@@ -383,7 +383,7 @@ ps.on('close', (code) => {
383383
});
384384

385385
grep.stdout.on('data', (data) => {
386-
console.log(`${data}`);
386+
console.log(data.toString());
387387
});
388388

389389
grep.stderr.on('data', (data) => {
@@ -467,8 +467,8 @@ const out = fs.openSync('./out.log', 'a');
467467
const err = fs.openSync('./out.log', 'a');
468468

469469
const child = spawn('prg', [], {
470-
detached: true,
471-
stdio: [ 'ignore', out, err ]
470+
detached: true,
471+
stdio: [ 'ignore', out, err ]
472472
});
473473

474474
child.unref();
@@ -860,7 +860,7 @@ as in this example:
860860
'use strict';
861861
const spawn = require('child_process').spawn;
862862

863-
let child = spawn('sh', ['-c',
863+
const child = spawn('sh', ['-c',
864864
`node -e "setInterval(() => {
865865
console.log(process.pid, 'is alive')
866866
}, 500);"`
@@ -1107,21 +1107,21 @@ const fs = require('fs');
11071107
const child_process = require('child_process');
11081108

11091109
const child = child_process.spawn('ls', {
1110-
stdio: [
1111-
0, // Use parents stdin for child
1112-
'pipe', // Pipe child's stdout to parent
1113-
fs.openSync('err.out', 'w') // Direct child's stderr to a file
1114-
]
1110+
stdio: [
1111+
0, // Use parent's stdin for child
1112+
'pipe', // Pipe child's stdout to parent
1113+
fs.openSync('err.out', 'w') // Direct child's stderr to a file
1114+
]
11151115
});
11161116

1117-
assert.equal(child.stdio[0], null);
1118-
assert.equal(child.stdio[0], child.stdin);
1117+
assert.strictEqual(child.stdio[0], null);
1118+
assert.strictEqual(child.stdio[0], child.stdin);
11191119

11201120
assert(child.stdout);
1121-
assert.equal(child.stdio[1], child.stdout);
1121+
assert.strictEqual(child.stdio[1], child.stdout);
11221122

1123-
assert.equal(child.stdio[2], null);
1124-
assert.equal(child.stdio[2], child.stderr);
1123+
assert.strictEqual(child.stdio[2], null);
1124+
assert.strictEqual(child.stdio[2], child.stderr);
11251125
```
11261126

11271127
### child.stdout

0 commit comments

Comments
 (0)