Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit b3f75ec

Browse filesBrowse files
jasnellFishrock123
authored andcommitted
doc: expand documentation for process.exit()
The fact that process.exit() interrupts pending async operations such as non-blocking i/o is becoming a bit more pronounced with the recent libuv update. This commit expands the documentation for `process.exit()` to explain clearly how it affects async operations. PR-URL: #6410 Reviewed-By: Сковорода Никита Андреевич <chalkerx@gmail.com> Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: Robert Lindstaedt <robert.lindstaedt@gmail.com>
1 parent 1b9fdba commit b3f75ec
Copy full SHA for b3f75ec

File tree

Expand file treeCollapse file tree

1 file changed

+52
-5
lines changed
Open diff view settings
Filter options
Expand file treeCollapse file tree

1 file changed

+52
-5
lines changed
Open diff view settings
Collapse file

‎doc/api/process.md‎

Copy file name to clipboardExpand all lines: doc/api/process.md
+52-5Lines changed: 52 additions & 5 deletions
  • Display the source diff
  • Display the rich diff
Original file line numberDiff line numberDiff line change
@@ -729,26 +729,73 @@ Example:
729729

730730
## process.exit([code])
731731

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.
734738

735739
To exit with a 'failure' code:
736740

737741
```js
738742
process.exit(1);
739743
```
740744

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+
```
742786

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()`.
743790

744791
## process.exitCode
745792

746793
A number which will be the process exit code, when the process either
747794
exits gracefully, or is exited via [`process.exit()`][] without specifying
748795
a code.
749796

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`.
752799

753800

754801
## process.getegid()

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.