Open In App

Node.js process.abort() Method

Last Updated : 10 May, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

The process.abort() property is an inbuilt application programming interface of the process module which is used to abort a running NodeJS process immediately. It also generates a core file.

Syntax:

process.abort()

Parameter: This function does not accept any parameter.

Return Type: It has a void return type.

Below examples illustrate the use of the process.abort() property in Node.js:

Example 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();


Run index.js file using below command:

node index.js

Output: We will see the following output on the console screen.

Start...
GeeksForGeeks
GeeksForGeeks
GeeksForGeeks
GeeksForGeeks
Abort trap: 6

Example 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();


Run index.js file using below command:

node index.js

Output: We will see the following output on the console screen.

Start...
GeeksForGeeks : 1 second
GeeksForGeeks : 2 second
GeeksForGeeks : 1 second
GeeksForGeeks : 1 second
GeeksForGeeks : 2 second
GeeksForGeeks : 1 second
Abort trap: 6

Reference: https://nodejs.org/api/process.html#process_process_abort



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads