Node.js process.abort() Method - GeeksforGeeks

10 May, 2021
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 methodconst 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 methodconst 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 secondsRunWithAbort();
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 methodconst 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 secondsRunWithAbort();
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
Node.js-Methods
Node.js-process-module
Picked
Node.js
Web Technologies
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Difference between dependencies, devDependencies and peerDependencies
How to connect Node.js with React.js ?
Node.js Export Module
Mongoose Populate() Method
Mongoose find() Function
Remove elements from a JavaScript Array
Convert a string to an integer in JavaScript
How to fetch data from an API in ReactJS ?
How to insert spaces/tabs in text using HTML/CSS?
Difference between var, let and const keywords in JavaScript