Node.js process.abort() 方法
process.abort() 属性是进程模块的内建应用程序编程接口,用于立即终止正在运行的 Node.js 进程。它还会生成一个核心文件。
语法:
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