@@ -880,10 +880,11 @@ added: v0.1.13
880
880
881
881
* ` code ` {Integer} The exit code. Defaults to ` 0 ` .
882
882
883
- The ` process.exit() ` method instructs Node.js to terminate the process as
884
- quickly as possible with the specified exit ` code ` . If the ` code ` is omitted,
885
- exit uses either the 'success' code ` 0 ` or the value of ` process.exitCode ` if
886
- specified.
883
+ The ` process.exit() ` method instructs Node.js to terminate the process
884
+ synchronously with an exit status of ` code ` . If ` code ` is omitted, exit uses
885
+ either the 'success' code ` 0 ` or the value of ` process.exitCode ` if it has been
886
+ set. Node.js will not terminate until all the [ ` 'exit' ` ] event listeners are
887
+ called.
887
888
888
889
To exit with a 'failure' code:
889
890
@@ -916,7 +917,7 @@ if (someConditionNotMet()) {
916
917
```
917
918
918
919
The reason this is problematic is because writes to ` process.stdout ` in Node.js
919
- are sometimes * non-blocking * and may occur over multiple ticks of the Node.js
920
+ are sometimes * asynchronous * and may occur over multiple ticks of the Node.js
920
921
event loop. Calling ` process.exit() ` , however, forces the process to exit
921
922
* before* those additional writes to ` stdout ` can be performed.
922
923
@@ -1511,23 +1512,11 @@ Android)
1511
1512
1512
1513
* {Stream}
1513
1514
1514
- The ` process.stderr ` property returns a [ Writable] [ ] stream equivalent to or
1515
- associated with ` stderr ` (fd ` 2 ` ).
1515
+ The ` process.stderr ` property returns a [ Writable] [ ] stream connected to
1516
+ ` stderr ` (fd ` 2 ` ).
1516
1517
1517
- Note: ` process.stderr ` and ` process.stdout ` differ from other Node.js streams
1518
- in several ways:
1519
- 1 . They cannot be closed ([ ` end() ` ] [ ] will throw).
1520
- 2 . They never emit the [ ` 'finish' ` ] [ ] event.
1521
- 3 . Writes _ can_ block when output is redirected to a file.
1522
- - Note that disks are fast and operating systems normally employ write-back
1523
- caching so this is very uncommon.
1524
- 4 . Writes on UNIX ** will** block by default if output is going to a TTY
1525
- (a terminal).
1526
- 5 . Windows functionality differs. Writes block except when output is going to a
1527
- TTY.
1528
-
1529
- To check if Node.js is being run in a TTY context, read the ` isTTY ` property
1530
- on ` process.stderr ` , ` process.stdout ` , or ` process.stdin ` :
1518
+ Note: ` process.stderr ` differs from other Node.js streams in important ways,
1519
+ see [ note on process I/O] [ ] for more information.
1531
1520
1532
1521
## process.stdin
1533
1522
@@ -1565,48 +1554,59 @@ must call `process.stdin.resume()` to read from it. Note also that calling
1565
1554
1566
1555
* {Stream}
1567
1556
1568
- The ` process.stdout ` property returns a [ Writable] [ ] stream equivalent to or
1569
- associated with ` stdout ` (fd ` 1 ` ).
1557
+ The ` process.stdout ` property returns a [ Writable] [ ] stream connected to
1558
+ ` stdout ` (fd ` 2 ` ).
1570
1559
1571
- For example:
1560
+ For example, to copy process.stdin to process.stdout :
1572
1561
1573
1562
``` js
1574
- console .log = (msg ) => {
1575
- process .stdout .write (` ${ msg} \n ` );
1576
- };
1563
+ process .stdin .pipe (process .stdout );
1577
1564
```
1578
1565
1579
- Note: ` process.stderr ` and ` process.stdout ` differ from other Node.js streams
1580
- in several ways:
1581
- 1 . They cannot be closed ([ ` end() ` ] [ ] will throw).
1582
- 2 . They never emit the [ ` 'finish' ` ] [ ] event.
1583
- 3 . Writes _ can_ block when output is redirected to a file.
1584
- - Note that disks are fast and operating systems normally employ write-back
1585
- caching so this is very uncommon.
1586
- 4 . Writes on UNIX ** will** block by default if output is going to a TTY
1587
- (a terminal).
1588
- 5 . Windows functionality differs. Writes block except when output is going to a
1589
- TTY.
1566
+ Note: ` process.stdout ` differs from other Node.js streams in important ways,
1567
+ see [ note on process I/O] [ ] for more information.
1568
+
1569
+ ### A note on process I/O
1590
1570
1591
- To check if Node.js is being run in a TTY context, read the ` isTTY ` property
1592
- on ` process.stderr ` , ` process.stdout ` , or ` process.stdin ` :
1571
+ ` process.stdout ` and ` process.stderr ` differ from other Node.js streams in
1572
+ important ways :
1593
1573
1594
- ### TTY Terminals and ` process.stdout `
1574
+ 1 . They are used internally by [ ` console.log() ` ] [ ] and [ ` console.error() ` ] [ ] ,
1575
+ respectively.
1576
+ 2 . They cannot be closed ([ ` end() ` ] [ ] will throw).
1577
+ 3 . They will never emit the [ ` 'finish' ` ] [ ] event.
1578
+ 4 . Writes may be synchronous depending on the what the stream is connected to
1579
+ and whether the system is Windows or Unix:
1580
+ - Files: * synchronous* on Windows and Linux
1581
+ - TTYs (Terminals): * asynchronous* on Windows, * synchronous* on Unix
1582
+ - Pipes (and sockets): * synchronous* on Windows, * asynchronous* on Unix
1595
1583
1596
- The ` process.stderr ` and ` process.stdout ` streams are blocking when outputting
1597
- to TTYs (terminals) on OS X as a workaround for the operating system's small,
1598
- 1kb buffer size. This is to prevent interleaving between ` stdout ` and ` stderr ` .
1584
+ These behaviours are partly for historical reasons, as changing them would
1585
+ create backwards incompatibility, but they are also expected by some users.
1599
1586
1600
- To check if Node.js is being run in a [ TTY] [ ] context, check the ` isTTY `
1601
- property on ` process.stderr ` , ` process.stdout ` , or ` process.stdin ` .
1587
+ Synchronous writes avoid problems such as output written with ` console.log() ` or
1588
+ ` console.write() ` being unexpectedly interleaved, or not written at all if
1589
+ ` process.exit() ` is called before an asynchronous write completes. See
1590
+ [ ` process.exit() ` ] [ ] for more information.
1591
+
1592
+ *** Warning*** : Synchronous writes block the event loop until the write has
1593
+ completed. This can be near instantaneous in the case of output to a file, but
1594
+ under high system load, pipes that are not being read at the receiving end, or
1595
+ with slow terminals or file systems, its possible for the event loop to be
1596
+ blocked often enough and long enough to have severe negative performance
1597
+ impacts. This may not be a problem when writing to an interactive terminal
1598
+ session, but consider this particularly careful when doing production logging to
1599
+ the process output streams.
1600
+
1601
+ To check if a stream is connected to a [ TTY] [ ] context, check the ` isTTY `
1602
+ property.
1602
1603
1603
1604
For instance:
1604
1605
``` console
1605
1606
$ node -p " Boolean(process.stdin.isTTY)"
1606
1607
true
1607
1608
$ echo " foo" | node -p " Boolean(process.stdin.isTTY)"
1608
1609
false
1609
-
1610
1610
$ node -p " Boolean(process.stdout.isTTY)"
1611
1611
true
1612
1612
$ node -p " Boolean(process.stdout.isTTY)" | cat
@@ -1758,6 +1758,7 @@ cases:
1758
1758
the high-order bit, and then contain the value of the signal code.
1759
1759
1760
1760
1761
+ [ `'exit'` ] : #process_event_exit
1761
1762
[ `'finish'` ] : stream.html#stream_event_finish
1762
1763
[ `'message'` ] : child_process.html#child_process_event_message
1763
1764
[ `'rejectionHandled'` ] : #process_event_rejectionhandled
@@ -1779,6 +1780,7 @@ cases:
1779
1780
[ `promise.catch()` ] : https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/catch
1780
1781
[ `require.main` ] : modules.html#modules_accessing_the_main_module
1781
1782
[ `setTimeout(fn, 0)` ] : timers.html#timers_settimeout_callback_delay_args
1783
+ [ note on process I/O ] : process.html#process_a_note_on_process_i_o
1782
1784
[ process_emit_warning ] : #process_process_emitwarning_warning_name_ctor
1783
1785
[ process_warning ] : #process_event_warning
1784
1786
[ Signal Events ] : #process_signal_events
0 commit comments