I wrote a code that ends the process under certain conditions, and when I write it as a
return, it ends abnormally due to an over-memory error, and
process.exit() ends normally.
P.S. My code is just only one function. So the two method quit the one function.
Can you explain the difference between the two methods when ending the process?
if(condition === true) process.exit();
if(condition === true) return;
- Explicitly calling
process.exitforcibly discards some pending asynchronous tasks.
Calling process.exit() will force the process to exit as quickly as possible even if there are still asynchronous operations pending that have not yet completed fully, including I/O operations to process.stdout and process.stderr.
beforeExitevent from emitting.
The ‘beforeExit’ event is emitted when Node.js empties its event loop and has no additional work to schedule. Normally, the Node.js process will exit when there is no work scheduled, but a listener registered on the ‘beforeExit’ event can make asynchronous calls, and thereby cause the Node.js process to continue.