Skip to content

Commit d3628d9

Browse files
vsemozhetbytitaloacasas
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 3270d4c commit d3628d9

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
// ...
@@ -391,7 +391,7 @@ ps.on('close', (code) => {
391391
});
392392

393393
grep.stdout.on('data', (data) => {
394-
console.log(`${data}`);
394+
console.log(data.toString());
395395
});
396396

397397
grep.stderr.on('data', (data) => {
@@ -475,8 +475,8 @@ const out = fs.openSync('./out.log', 'a');
475475
const err = fs.openSync('./out.log', 'a');
476476

477477
const child = spawn('prg', [], {
478-
detached: true,
479-
stdio: [ 'ignore', out, err ]
478+
detached: true,
479+
stdio: [ 'ignore', out, err ]
480480
});
481481

482482
child.unref();
@@ -876,7 +876,7 @@ as in this example:
876876
'use strict';
877877
const spawn = require('child_process').spawn;
878878

879-
let child = spawn('sh', ['-c',
879+
const child = spawn('sh', ['-c',
880880
`node -e "setInterval(() => {
881881
console.log(process.pid, 'is alive')
882882
}, 500);"`
@@ -1123,21 +1123,21 @@ const fs = require('fs');
11231123
const child_process = require('child_process');
11241124

11251125
const child = child_process.spawn('ls', {
1126-
stdio: [
1127-
0, // Use parents stdin for child
1128-
'pipe', // Pipe child's stdout to parent
1129-
fs.openSync('err.out', 'w') // Direct child's stderr to a file
1130-
]
1126+
stdio: [
1127+
0, // Use parent's stdin for child
1128+
'pipe', // Pipe child's stdout to parent
1129+
fs.openSync('err.out', 'w') // Direct child's stderr to a file
1130+
]
11311131
});
11321132

1133-
assert.equal(child.stdio[0], null);
1134-
assert.equal(child.stdio[0], child.stdin);
1133+
assert.strictEqual(child.stdio[0], null);
1134+
assert.strictEqual(child.stdio[0], child.stdin);
11351135

11361136
assert(child.stdout);
1137-
assert.equal(child.stdio[1], child.stdout);
1137+
assert.strictEqual(child.stdio[1], child.stdout);
11381138

1139-
assert.equal(child.stdio[2], null);
1140-
assert.equal(child.stdio[2], child.stderr);
1139+
assert.strictEqual(child.stdio[2], null);
1140+
assert.strictEqual(child.stdio[2], child.stderr);
11411141
```
11421142

11431143
### child.stdout

0 commit comments

Comments
 (0)