Tuesday, 2 October 2018

Node.js: process.exit: Exit current process

process.exit([code])
It exits the process synchronously with an exit status of code.

HelloWorld.js
console.log("Hello World");
console.log("Welcome to node.js programming");

/*
 * If you do not pass exit code, exit uses either the 'success' code 0 or the
 * value of process.exitCode if it has been set
 */
process.exit();

console.log("This message do not print");


Output
Hello World
Welcome to node.js programming

Adding a listener on process exit
/* Adding a listener on process exit */
process.on("exit", function() {

         console.log("Exit initialted by application");

});

HelloWorld.js
/* Adding a listener on process exit */
process.on("exit", function() {

 console.log("Exit initiated by application");

});

console.log("Hello World");
console.log("Welcome to node.js programming");

/*
 * If you do not pass exit code, exit uses either the 'success' code 0 or the
 * value of process.exitCode if it has been set
 */
process.exit();

console.log("This message do not print");


Output
Hello World
Welcome to node.js programming
Exit initiated by application




Previous                                                 Next                                                 Home

No comments:

Post a Comment