'beforeExit' 事件

当 Node.js 清空了事件循环且不再添加额外的工作时触发。 正常情况下,如果不再添加额外的工作,则 Node.js 进程会退出。 如果 'beforeExit' 事件注册的监听器中含有异步调用,则 Node.js 进程会继续运行。

如果进程被显式地终止,比如调用 process.exit() 或抛出未捕获的异常,则不会触发 'beforeExit' 事件。

除非需要添加额外的工作,否则不要使用 'beforeExit' 事件代替 'exit' 事件。

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.

The listener callback function is invoked with the value of process.exitCode passed as the only argument.

The 'beforeExit' event is not emitted for conditions causing explicit termination, such as calling process.exit() or uncaught exceptions.

The 'beforeExit' should not be used as an alternative to the 'exit' event unless the intention is to schedule additional work.