beforeExit and exit events
Process events: beforeExit fires when the event loop has run out of work and may schedule more, while exit fires just before the process ends and allows only synchronous code.
In more detail
beforeExit is not emitted when process.exit() is called or on an uncaught exception. Inside exit handlers, asynchronous work such as a network request never completes because the loop has already stopped.
Related terms
- process object — The global object in Node.js that describes the running process and lets you read its environment and arguments, measure its memory, listen for signals and exit.
- process.exit() — A Node.js function that ends the process at once with the exit code you give, skipping any work still waiting on the event loop.
- synchronous code — Code that runs start to finish without pausing, so the next line waits for it to complete.
Taught in
- Close Phase & Signal Dispatch: Sockets & Teardown — Node.js Internals & Backend Services
- Global Error Trapping: uncaughtException & Exits — Node.js Internals & Backend Services