@@ -729,26 +729,73 @@ Example: |
729 | 729 |
|
730 | 730 | ## process.exit([code]) |
731 | 731 |
|
732 | | -Ends the process with the specified `code`. If omitted, exit uses the |
733 | | -'success' code `0`. |
| 732 | +* `code` {Integer} The exit code. Defaults to `0`. |
| 733 | + |
| 734 | +The `process.exit()` method instructs Node.js to terminate the process as |
| 735 | +quickly as possible with the specified exit `code`. If the `code` is omitted, |
| 736 | +exit uses either the 'success' code `0` or the value of `process.exitCode` if |
| 737 | +specified. |
734 | 738 |
|
735 | 739 | To exit with a 'failure' code: |
736 | 740 |
|
737 | 741 | ```js |
738 | 742 | process.exit(1); |
739 | 743 | ``` |
740 | 744 |
|
741 | | -The shell that executed Node.js should see the exit code as 1. |
| 745 | +The shell that executed Node.js should see the exit code as `1`. |
| 746 | + |
| 747 | +It is important to note that calling `process.exit()` will force the process to |
| 748 | +exit as quickly as possible *even if there are still asynchronous operations |
| 749 | +pending* that have not yet completed fully, *including* I/O operations to |
| 750 | +`process.stdout` and `process.stderr`. |
| 751 | + |
| 752 | +In most situations, it is not actually necessary to call `process.exit()` |
| 753 | +explicitly. The Node.js process will exit on it's own *if there is no additional |
| 754 | +work pending* in the event loop. The `process.exitCode` property can be set to |
| 755 | +tell the process which exit code to use when the process exits gracefully. |
| 756 | + |
| 757 | +For instance, the following example illustrates a *misuse* of the |
| 758 | +`process.exit()` method that could lead to data printed to stdout being |
| 759 | +truncated and lost: |
| 760 | + |
| 761 | +```js |
| 762 | +// This is an example of what *not* to do: |
| 763 | +if (someConditionNotMet()) { |
| 764 | + printUsageToStdout(); |
| 765 | + process.exit(1); |
| 766 | +} |
| 767 | +``` |
| 768 | + |
| 769 | +The reason this is problematic is because writes to `process.stdout` in Node.js |
| 770 | +are *non-blocking* and may occur over multiple ticks of the Node.js event loop. |
| 771 | +Calling `process.exit()`, however, forces the process to exit *before* those |
| 772 | +additional writes to `stdout` can be performed. |
| 773 | + |
| 774 | +Rather than calling `process.exit()` directly, the code *should* set the |
| 775 | +`process.exitCode` and allow the process to exit naturally by avoiding |
| 776 | +scheduling any additional work for the event loop: |
| 777 | + |
| 778 | +```js |
| 779 | +// How to properly set the exit code while letting |
| 780 | +// the process exit gracefully. |
| 781 | +if (someConditionNotMet()) { |
| 782 | + printUsageToStdout(); |
| 783 | + process.exitCode = 1; |
| 784 | +} |
| 785 | +``` |
742 | 786 |
|
| 787 | +If it is necessary to terminate the Node.js process due to an error condition, |
| 788 | +throwing an *uncaught* error and allowing the process to terminate accordingly |
| 789 | +is safer than calling `process.exit()`. |
743 | 790 |
|
744 | 791 | ## process.exitCode |
745 | 792 |
|
746 | 793 | A number which will be the process exit code, when the process either |
747 | 794 | exits gracefully, or is exited via [`process.exit()`][] without specifying |
748 | 795 | a code. |
749 | 796 |
|
750 | | -Specifying a code to [`process.exit(code)`][`process.exit()`] will override any previous |
751 | | -setting of `process.exitCode`. |
| 797 | +Specifying a code to [`process.exit(code)`][`process.exit()`] will override any |
| 798 | +previous setting of `process.exitCode`. |
752 | 799 |
|
753 | 800 |
|
754 | 801 | ## process.getegid() |
|