@@ -859,10 +859,11 @@ added: v0.1.13
859
859
860
860
* ` code ` {Integer} The exit code. Defaults to ` 0 ` .
861
861
862
- The ` process.exit() ` method instructs Node.js to terminate the process as
863
- quickly as possible with the specified exit ` code ` . If the ` code ` is omitted,
864
- exit uses either the 'success' code ` 0 ` or the value of ` process.exitCode ` if
865
- specified.
862
+ The ` process.exit() ` method instructs Node.js to terminate the process
863
+ synchronously with an exit status of ` code ` . If ` code ` is omitted, exit uses
864
+ either the 'success' code ` 0 ` or the value of ` process.exitCode ` if it has been
865
+ set. Node.js will not terminate until all the [ ` 'exit' ` ] event listeners are
866
+ called.
866
867
867
868
To exit with a 'failure' code:
868
869
@@ -895,7 +896,7 @@ if (someConditionNotMet()) {
895
896
```
896
897
897
898
The reason this is problematic is because writes to ` process.stdout ` in Node.js
898
- are sometimes * non-blocking * and may occur over multiple ticks of the Node.js
899
+ are sometimes * asynchronous * and may occur over multiple ticks of the Node.js
899
900
event loop. Calling ` process.exit() ` , however, forces the process to exit
900
901
* before* those additional writes to ` stdout ` can be performed.
901
902
@@ -1488,23 +1489,11 @@ Android)
1488
1489
1489
1490
* {Stream}
1490
1491
1491
- The ` process.stderr ` property returns a [ Writable] [ ] stream equivalent to or
1492
- associated with ` stderr ` (fd ` 2 ` ).
1492
+ The ` process.stderr ` property returns a [ Writable] [ ] stream connected to
1493
+ ` stderr ` (fd ` 2 ` ).
1493
1494
1494
- Note: ` process.stderr ` and ` process.stdout ` differ from other Node.js streams
1495
- in several ways:
1496
- 1 . They cannot be closed ([ ` end() ` ] [ ] will throw).
1497
- 2 . They never emit the [ ` 'finish' ` ] [ ] event.
1498
- 3 . Writes _ can_ block when output is redirected to a file.
1499
- - Note that disks are fast and operating systems normally employ write-back
1500
- caching so this is very uncommon.
1501
- 4 . Writes on UNIX ** will** block by default if output is going to a TTY
1502
- (a terminal).
1503
- 5 . Windows functionality differs. Writes block except when output is going to a
1504
- TTY.
1505
-
1506
- To check if Node.js is being run in a TTY context, read the ` isTTY ` property
1507
- on ` process.stderr ` , ` process.stdout ` , or ` process.stdin ` :
1495
+ Note: ` process.stderr ` differs from other Node.js streams in important ways,
1496
+ see [ note on process I/O] [ ] for more information.
1508
1497
1509
1498
## process.stdin
1510
1499
@@ -1542,48 +1531,59 @@ must call `process.stdin.resume()` to read from it. Note also that calling
1542
1531
1543
1532
* {Stream}
1544
1533
1545
- The ` process.stdout ` property returns a [ Writable] [ ] stream equivalent to or
1546
- associated with ` stdout ` (fd ` 1 ` ).
1534
+ The ` process.stdout ` property returns a [ Writable] [ ] stream connected to
1535
+ ` stdout ` (fd ` 2 ` ).
1547
1536
1548
- For example:
1537
+ For example, to copy process.stdin to process.stdout :
1549
1538
1550
1539
``` js
1551
- console .log = (msg ) => {
1552
- process .stdout .write (` ${ msg} \n ` );
1553
- };
1540
+ process .stdin .pipe (process .stdout );
1554
1541
```
1555
1542
1556
- Note: ` process.stderr ` and ` process.stdout ` differ from other Node.js streams
1557
- in several ways:
1558
- 1 . They cannot be closed ([ ` end() ` ] [ ] will throw).
1559
- 2 . They never emit the [ ` 'finish' ` ] [ ] event.
1560
- 3 . Writes _ can_ block when output is redirected to a file.
1561
- - Note that disks are fast and operating systems normally employ write-back
1562
- caching so this is very uncommon.
1563
- 4 . Writes on UNIX ** will** block by default if output is going to a TTY
1564
- (a terminal).
1565
- 5 . Windows functionality differs. Writes block except when output is going to a
1566
- TTY.
1543
+ Note: ` process.stdout ` differs from other Node.js streams in important ways,
1544
+ see [ note on process I/O] [ ] for more information.
1545
+
1546
+ ### A note on process I/O
1567
1547
1568
- To check if Node.js is being run in a TTY context, read the ` isTTY ` property
1569
- on ` process.stderr ` , ` process.stdout ` , or ` process.stdin ` :
1548
+ ` process.stdout ` and ` process.stderr ` differ from other Node.js streams in
1549
+ important ways :
1570
1550
1571
- ### TTY Terminals and ` process.stdout `
1551
+ 1 . They are used internally by [ ` console.log() ` ] [ ] and [ ` console.error() ` ] [ ] ,
1552
+ respectively.
1553
+ 2 . They cannot be closed ([ ` end() ` ] [ ] will throw).
1554
+ 3 . They will never emit the [ ` 'finish' ` ] [ ] event.
1555
+ 4 . Writes may be synchronous depending on the what the stream is connected to
1556
+ and whether the system is Windows or Unix:
1557
+ - Files: * synchronous* on Windows and Linux
1558
+ - TTYs (Terminals): * asynchronous* on Windows, * synchronous* on Unix
1559
+ - Pipes (and sockets): * synchronous* on Windows, * asynchronous* on Unix
1572
1560
1573
- The ` process.stderr ` and ` process.stdout ` streams are blocking when outputting
1574
- to TTYs (terminals) on OS X as a workaround for the operating system's small,
1575
- 1kb buffer size. This is to prevent interleaving between ` stdout ` and ` stderr ` .
1561
+ These behaviours are partly for historical reasons, as changing them would
1562
+ create backwards incompatibility, but they are also expected by some users.
1576
1563
1577
- To check if Node.js is being run in a [ TTY] [ ] context, check the ` isTTY `
1578
- property on ` process.stderr ` , ` process.stdout ` , or ` process.stdin ` .
1564
+ Synchronous writes avoid problems such as output written with ` console.log() ` or
1565
+ ` console.write() ` being unexpectedly interleaved, or not written at all if
1566
+ ` process.exit() ` is called before an asynchronous write completes. See
1567
+ [ ` process.exit() ` ] [ ] for more information.
1568
+
1569
+ *** Warning*** : Synchronous writes block the event loop until the write has
1570
+ completed. This can be near instantaneous in the case of output to a file, but
1571
+ under high system load, pipes that are not being read at the receiving end, or
1572
+ with slow terminals or file systems, its possible for the event loop to be
1573
+ blocked often enough and long enough to have severe negative performance
1574
+ impacts. This may not be a problem when writing to an interactive terminal
1575
+ session, but consider this particularly careful when doing production logging to
1576
+ the process output streams.
1577
+
1578
+ To check if a stream is connected to a [ TTY] [ ] context, check the ` isTTY `
1579
+ property.
1579
1580
1580
1581
For instance:
1581
1582
``` console
1582
1583
$ node -p " Boolean(process.stdin.isTTY)"
1583
1584
true
1584
1585
$ echo " foo" | node -p " Boolean(process.stdin.isTTY)"
1585
1586
false
1586
-
1587
1587
$ node -p " Boolean(process.stdout.isTTY)"
1588
1588
true
1589
1589
$ node -p " Boolean(process.stdout.isTTY)" | cat
@@ -1737,6 +1737,7 @@ cases:
1737
1737
the high-order bit, and then contain the value of the signal code.
1738
1738
1739
1739
1740
+ [ `'exit'` ] : #process_event_exit
1740
1741
[ `'finish'` ] : stream.html#stream_event_finish
1741
1742
[ `'message'` ] : child_process.html#child_process_event_message
1742
1743
[ `'rejectionHandled'` ] : #process_event_rejectionhandled
@@ -1758,6 +1759,7 @@ cases:
1758
1759
[ `promise.catch()` ] : https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/catch
1759
1760
[ `require.main` ] : modules.html#modules_accessing_the_main_module
1760
1761
[ `setTimeout(fn, 0)` ] : timers.html#timers_settimeout_callback_delay_args
1762
+ [ note on process I/O ] : process.html#process_a_note_on_process_i_o
1761
1763
[ process_emit_warning ] : #process_process_emitwarning_warning_name_ctor
1762
1764
[ process_warning ] : #process_event_warning
1763
1765
[ Signal Events ] : #process_signal_events
0 commit comments