“ beforeExit”是流程模塊中流程類的事件,當Node.js清空其事件循環且沒有其他工作要安排時會發出該事件。
用法:
Event:'beforeExit'
參數:此事件不接受任何參數作為參數。
返回值:此事件隻返回回調函數以進行進一步的操作。
範例1:
index.js
// Node.js program to demonstrate the
// Process 'beforeExit' Event
// Importing process module
const process = require('process');
// Event 'beforeExit'
process.on('beforeExit', (code) => {
console.log('Process beforeExit event with code:', code);
});
// Event 'exit'
process.on('exit', (code) => {
console.log('Process exit event with code:', code);
});
// Display the first message
console.log('This message is displayed first.');
使用以下命令運行index.js文件:
node index.js
輸出:
This message is displayed first. Process beforeExit event with code: 0 Process exit event with code: 0
範例2:
index.js
// Node.js program to demonstrate the
// Process 'beforeExit' Event
// Importing process module
const process = require('process');
// Updating the exit code
process.exitCode = 100;
// Event 'beforeExit'
process.on('beforeExit', (code) => {
console.log('Process beforeExit event with code:', code);
});
// Display the first message
console.log('This message is displayed first.');
使用以下命令運行index.js文件:
node index.js
輸出:
This message is displayed first. Process beforeExit event with code: 100
參考: https://nodejs.org/dist/latest-v16.x/docs/api/process.html#process_event_beforeexit
相關用法
- Node.js process.nextTick()用法及代碼示例
- jQuery UI dialog create(event, ui)用法及代碼示例
- jQuery UI dialog close(event, ui)用法及代碼示例
- jQuery UI dialog resizeStart(event, ui)用法及代碼示例
- jQuery UI dialog dragStop(event, ui)用法及代碼示例
- jQuery UI dialog dragStart(event, ui)用法及代碼示例
- jQuery UI dialog resize(event,ui)用法及代碼示例
- jQuery UI dialog open(event,ui)用法及代碼示例
- jQuery UI dialog focus(event,ui)用法及代碼示例
- Node.js Process exit用法及代碼示例
- Node.js Process message用法及代碼示例
- Node.js Process warning用法及代碼示例
- Node.js Process uncaughtException用法及代碼示例
- Node.js Process multipleResolves用法及代碼示例
注:本文由純淨天空篩選整理自RohitPrasad3大神的英文原創作品 Node.js Process beforeExit Event。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。