Node.js process.abort() Method
Last Updated :
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
const RunWithAbort = () => {
console.log( 'Start...' );
setInterval(( function () {
return console.log( 'GeeksForGeeks' );
}), 1000);
setTimeout(( function () {
return process.abort();
}), 5000);
};
const RunWithoutAbort = () => {
console.log( 'Start...' );
setInterval(( function () {
return console.log( 'GeeksForGeeks' );
}), 1000);
};
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
const RunWithAbort = () => {
console.log( 'Start...' );
setInterval(( function () {
return console.log( 'GeeksForGeeks : 1 second' );
}), 1000);
setInterval(( function () {
return console.log( 'GeeksForGeeks : 2 second' );
}), 2000);
setTimeout(( function () {
return process.abort();
}), 5000);
};
const RunWithoutAbort = () => {
console.log( 'Start...' );
setInterval(( function () {
return console.log( 'GeeksForGeeks' );
}), 1000);
};
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
Share your thoughts in the comments
Please Login to comment...