Skip to content

Commit e72ea0d

Browse files
committed
doc: modernize and fix code examples in process.md
* Replace `var` by `const`. * Use `console.error()`. * Fix example file name mismatch. * Update output examples. * Fix output readability (add a line break, remove an unnecessary duplicate word). PR-URL: #12381 Reviewed-By: Benjamin Gruenbaum <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Luigi Pinca <[email protected]> Reviewed-By: Sakthipriyan Vairamani <[email protected]>
1 parent e1d1487 commit e72ea0d

File tree

1 file changed

+15
-15
lines changed

1 file changed

+15
-15
lines changed

doc/api/process.md

+15-15
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ For example:
162162

163163
```js
164164
process.on('uncaughtException', (err) => {
165-
fs.writeSync(1, `Caught exception: ${err}`);
165+
fs.writeSync(1, `Caught exception: ${err}\n`);
166166
});
167167

168168
setTimeout(() => {
@@ -231,7 +231,7 @@ For example:
231231

232232
```js
233233
process.on('unhandledRejection', (reason, p) => {
234-
console.log('Unhandled Rejection at: Promise', p, 'reason:', reason);
234+
console.log('Unhandled Rejection at:', p, 'reason:', reason);
235235
// application specific logging, throwing an error, or other logic here
236236
});
237237

@@ -249,7 +249,7 @@ function SomeResource() {
249249
this.loaded = Promise.reject(new Error('Resource not yet loaded!'));
250250
}
251251

252-
var resource = new SomeResource();
252+
const resource = new SomeResource();
253253
// no .catch or .then on resource.loaded for at least a turn
254254
```
255255

@@ -301,16 +301,16 @@ $ node
301301
> events.defaultMaxListeners = 1;
302302
> process.on('foo', () => {});
303303
> process.on('foo', () => {});
304-
> (node:38638) Warning: Possible EventEmitter memory leak detected. 2 foo
305-
... listeners added. Use emitter.setMaxListeners() to increase limit
304+
> (node:38638) MaxListenersExceededWarning: Possible EventEmitter memory leak
305+
detected. 2 foo listeners added. Use emitter.setMaxListeners() to increase limit
306306
```
307307

308308
In contrast, the following example turns off the default warning output and
309309
adds a custom handler to the `'warning'` event:
310310

311311
```txt
312312
$ node --no-warnings
313-
> var p = process.on('warning', (warning) => console.warn('Do not do that!'));
313+
> const p = process.on('warning', (warning) => console.warn('Do not do that!'));
314314
> events.defaultMaxListeners = 1;
315315
> process.on('foo', () => {});
316316
> process.on('foo', () => {});
@@ -452,14 +452,14 @@ process.argv.forEach((val, index) => {
452452
Launching the Node.js process as:
453453

454454
```console
455-
$ node process-2.js one two=three four
455+
$ node process-args.js one two=three four
456456
```
457457

458458
Would generate the output:
459459

460460
```text
461461
0: /usr/local/bin/node
462-
1: /Users/mjr/work/node/process-2.js
462+
1: /Users/mjr/work/node/process-args.js
463463
2: one
464464
3: two=three
465465
4: four
@@ -511,7 +511,7 @@ try {
511511
console.log(`New directory: ${process.cwd()}`);
512512
}
513513
catch (err) {
514-
console.log(`chdir: ${err}`);
514+
console.error(`chdir: ${err}`);
515515
}
516516
```
517517

@@ -668,7 +668,7 @@ process.emitWarning('Something Happened!', 'CustomWarning');
668668

669669
```js
670670
process.emitWarning('Something happened!', 'CustomWarning', 'WARN001');
671-
// Emits: (node:56338) CustomWarning [WARN001]: Something Happened!
671+
// Emits: (node:56338) [WARN001] CustomWarning: Something happened!
672672
```
673673

674674
In each of the previous examples, an `Error` object is generated internally by
@@ -696,7 +696,7 @@ myWarning.name = 'CustomWarning';
696696
myWarning.code = 'WARN001';
697697

698698
process.emitWarning(myWarning);
699-
// Emits: (node:56338) CustomWarning [WARN001]: Warning! Something Happened!
699+
// Emits: (node:56338) [WARN001] CustomWarning: Warning! Something happened!
700700
```
701701

702702
A `TypeError` is thrown if `warning` is anything other than a string or `Error`
@@ -1053,11 +1053,11 @@ drift. The primary use is for measuring performance between intervals:
10531053

10541054
```js
10551055
const NS_PER_SEC = 1e9;
1056-
var time = process.hrtime();
1056+
const time = process.hrtime();
10571057
// [ 1800216, 25 ]
10581058

10591059
setTimeout(() => {
1060-
var diff = process.hrtime(time);
1060+
const diff = process.hrtime(time);
10611061
// [ 1, 552 ]
10621062

10631063
console.log(`Benchmark took ${diff[0] * NS_PER_SEC + diff[1]} nanoseconds`);
@@ -1232,7 +1232,7 @@ function MyThing(options) {
12321232
});
12331233
}
12341234

1235-
var thing = new MyThing();
1235+
const thing = new MyThing();
12361236
thing.getReadyForStuff();
12371237

12381238
// thing.startDoingStuff() gets called now, not before.
@@ -1535,7 +1535,7 @@ For example:
15351535
process.stdin.setEncoding('utf8');
15361536

15371537
process.stdin.on('readable', () => {
1538-
var chunk = process.stdin.read();
1538+
const chunk = process.stdin.read();
15391539
if (chunk !== null) {
15401540
process.stdout.write(`data: ${chunk}`);
15411541
}

0 commit comments

Comments
 (0)