CodePark/ questions / Errors, process & signals
START PRACTICINGPRACTICE
Backend question · Node.js › Errors, process & signals

Why do the Node docs recommend setting process.exitCode = 1 and letting the process end naturally instead of calling process.exit(1)?

process.exit() ends the process immediately, so pending async work such as writes to stdout can be lost

process.exit() ends the process as soon as possible. It does this even when asynchronous work is still pending, such as output that is still being written to stdout. That output can be lost.

Setting
process.exitCode = 1 does not stop anything. The event loop finishes its remaining work, and then the process exits with that code.

The other options are wrong:

• "
process.exit() is deprecated": it is not deprecated.
• "
process.exit() ignores the exit code you pass": it does use the code you pass.
• "
process.exit() waits for all timers": it does the opposite. It does not wait for anything.