process.abort()屬性是流程模塊的內置應用程序編程接口,用於立即中止正在運行的NodeJS流程。它還會生成一個核心文件。
用法:
process.abort()
參數:此函數不接受任何參數。
返回類型:它具有void返回類型。
以下示例說明了Node.js中process.abort()屬性的用法:
範例1:
index.js
// Function to illustrate abort method
const RunWithAbort = () => {
console.log('Start...');
// It prints GeeksForGeeks after every 1 second
setInterval((function() {
return console.log('GeeksForGeeks');
}), 1000);
// It calls process.abort() after 5 seconds
setTimeout((function() {
return process.abort();
}), 5000);
};
// Function to illustrate working of above function
// without abort method
const RunWithoutAbort = () => {
console.log('Start...');
// It prints GeeksForGeeks after every 1 second
setInterval((function() {
return console.log('GeeksForGeeks');
}), 1000);
};
// Uncomment below line to call RunWithoutAbort
// function it will run in infinitely
// RunWithoutAbort();
// Call RunWithAbort function
// it will abort after 5 seconds
RunWithAbort();
使用以下命令運行index.js文件:
node index.js
輸出:我們將在控製台屏幕上看到以下輸出。
Start... GeeksForGeeks GeeksForGeeks GeeksForGeeks GeeksForGeeks Abort trap:6
範例2:
index.js
// Function to illustrate abort method
const RunWithAbort = () => {
console.log('Start...');
// It prints GeeksForGeeks after every 1 second
setInterval((function() {
return console.log('GeeksForGeeks:1 second');
}), 1000);
// It prints GeeksForGeeks after every 2 seconds
setInterval((function() {
return console.log('GeeksForGeeks:2 second');
}), 2000);
// It calls process.abort() after 5 seconds
setTimeout((function() {
return process.abort();
}), 5000);
};
const RunWithoutAbort = () => {
console.log('Start...');
// It prints GeeksForGeeks after every 1 second
setInterval((function() {
return console.log('GeeksForGeeks');
}), 1000);
};
// Uncomment below line to call RunWithoutAbort
// function it will run in infinitely
// RunWithoutAbort();
// Call RunWithAbort function
// it will abort after 5 seconds
RunWithAbort();
使用以下命令運行index.js文件:
node index.js
輸出:我們將在控製台屏幕上看到以下輸出。
Start... GeeksForGeeks:1 second GeeksForGeeks:2 second GeeksForGeeks:1 second GeeksForGeeks:1 second GeeksForGeeks:2 second GeeksForGeeks:1 second Abort trap:6
參考: https://nodejs.org/api/process.html#process_process_abort
相關用法
- Node.js console.timeLog()用法及代碼示例
- Node.js x509.toLegacyObject()用法及代碼示例
- Node.js fs.fsyncSync()用法及代碼示例
- Node.js process.nextTick()用法及代碼示例
- Node.js GM charcoal()用法及代碼示例
- Node.js GM blur()用法及代碼示例
注:本文由純淨天空篩選整理自adityapande88大神的英文原創作品 Node.js process.abort() Method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。