📜  Node.js process.abort() 方法

📅  最后修改于: 2022-05-13 01:56:34.074000             🧑  作者: Mango

Node.js process.abort() 方法

process.abort()属性是 process 模块的内置应用程序编程接口,用于立即中止正在运行的 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
// 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
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