Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the difference between abort() and exit() in Node?

Tags:

node.js

I noticed that both process.exit() and process.abort() both stop a script. What are the differences between the two besides that one logs Aborted?

like image 846
baranskistad Avatar asked Oct 09 '16 17:10

baranskistad


2 Answers

process.abort() stops the process immediately.

process.exit([exitCode]) method instructs Node.js to terminate the process as quickly as possible. You can also specify an exit code.

For exit codes:

  • 0 means the process was exited successfully.
  • 1 means it ended abnormally.
  • When omitted, 0 is the default value.
like image 123
Edmar Miyake Avatar answered Oct 22 '22 11:10

Edmar Miyake


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.

The proper recommended way to exit from process is to set the exit code and allow process to exit naturally because calling process.exit() forces the process to exit before any additional writes to stdout can be performed.

process.exitCode = 1;

If it is necessary to terminate the Node.js process due to an error condition, throwing an uncaught error and allowing the process to terminate accordingly is safer than calling process.exit().

The process.abort() method causes the Node.js process to exit immediately and generate a core file.

like image 42
notionquest Avatar answered Oct 22 '22 11:10

notionquest