事件:'beforeExit'
添加于:v0.11.12
'beforeExit'
事件在 Node.js 清空其事件循环并且没有其他工作要安排时发出。通常情况下,Node.js 进程会在没有安排工作时退出,但在'beforeExit'
事件上注册的侦听器可以进行异步调用,从而使 Node.js 进程继续。
使用作为唯一参数传递的
的值调用侦听器回调函数。process.exitCode
'beforeExit'
事件不会针对导致显式终止的条件发出,例如调用
或未捕获的异常。process.exit()
'beforeExit'
不应用作'exit'
事件的替代,除非打算安排额外的工作。
import process from 'node:process'; process.on('beforeExit', (code) => { console.log('Process beforeExit event with code: ', code); }); process.on('exit', (code) => { console.log('Process exit event with code: ', code); }); console.log('This message is displayed first.'); // Prints: // This message is displayed first. // Process beforeExit event with code: 0 // Process exit event with code: 0
const process = require('node:process'); process.on('beforeExit', (code) => { console.log('Process beforeExit event with code: ', code); }); process.on('exit', (code) => { console.log('Process exit event with code: ', code); }); console.log('This message is displayed first.'); // Prints: // This message is displayed first. // Process beforeExit event with code: 0 // Process exit event with code: 0
相关用法
- Node.js tls.Server 'keylog'事件用法及代码示例
- Node.js http.Server 'clientError'事件用法及代码示例
- Node.js cluste 'disconnect'事件用法及代码示例
- Node.js proces 'exit'事件用法及代码示例
- Node.js stream.Writable 'pipe'事件用法及代码示例
- Node.js stream.Readable 'end'事件用法及代码示例
- Node.js cluste 'fork'事件用法及代码示例
- Node.js stream.Writable 'unpipe'事件用法及代码示例
- Node.js Http2Session 'remoteSettings'事件用法及代码示例
- Node.js Worker 'listening'事件用法及代码示例
- Node.js tls.Server 'resumeSession'事件用法及代码示例
- Node.js InterfaceConstructor 'pause'事件用法及代码示例
- Node.js fs.FSWatcher 'change'事件用法及代码示例
- Node.js stream.Readable 'data'事件用法及代码示例
- Node.js http.ClientRequest 'connect'事件用法及代码示例
- Node.js proces 'uncaughtException'事件用法及代码示例
- Node.js Http2Session 'localSettings'事件用法及代码示例
- Node.js REPLServer 'exit'事件用法及代码示例
- Node.js cluste 'online'事件用法及代码示例
- Node.js tls.TLSSocket 'session'事件用法及代码示例
- Node.js Worker 'exit'事件用法及代码示例
- Node.js cluste 'exit'事件用法及代码示例
- Node.js Http2Stream 'trailers'事件用法及代码示例
- Node.js MessagePort 'close'事件用法及代码示例
- Node.js http.ClientRequest 'information'事件用法及代码示例
注:本文由纯净天空筛选整理自nodejs.org大神的英文原创作品 'beforeExit'事件。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。