當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


Node.js Process beforeExit用法及代碼示例


“ 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

相關用法


注:本文由純淨天空篩選整理自RohitPrasad3大神的英文原創作品 Node.js Process beforeExit Event。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。