Nodejs-process Module

What is Nodejs Process Module?

const process = require('process');

Process Events

  1. beforeExit.
  2. disconnect.
  3. exit.
  4. message.
  5. multipleResolves.
  6. unhandledRejection.
  7. uncaughtException.
  8. warning.

1. beforeExit

const x = 10;
const y = 10;
function sum(a, b) {
return a + b;
}
console.log(`sum of ${x} and ${y} is`, sum(x, y));
process.on('beforeExit', (code) => {
console.log("process before exit event with code ", code)
})
const x = 10;
const y = 10;
function sum(a, b) {
process.exit()
return a + b;}
console.log(`sum of ${x} and ${y} is`, sum(x, y));
process.on('beforeExit', (code) => {
console.log("process before exit event with code ", code)
})
const x = 10;
const y = 10;
function sum(a, b) {
throw 1;
return a + b;
}
console.log(`sum of ${x} and ${y} is`, sum(x, y));
process.on('beforeExit', (code) => {
console.log("process before exit event with code ", code)})

2. disconnect

*Parent Processconst{fork}=require('child_process');
const child=fork('./child.js');
child.send("Hey child ,Iam parent");
child.disconnect();
*Child Processprocess.on("message", (message) => {
console.log(message);
});
process.on("disconnect", (messag) => {
console.log("Parent Disconnected with me");
});
Note* Both the child process and parent process code should be in seperate js file

3. exit

*Parent Process
const { fork } = require('child_process');
const child = fork('child.js');
child.send("First Message from parent");
child.send("Second Message from parent");
*Child Process
process.on('message',(message)=>{
console.log(message);
process.exit();})
Note* Both the child process and parent process code should be in seperate js file
*Child Processprocess.on('message',(message)=>{
console.log(message);
process.exit();})
process.on('exit',()=>{
console.log("child process exited");
})

4. message

process.on("message",(message)=>{
console.log(message);
})

5. multipleResolves

  1. Resolved more than once.
  2. Rejected more than once.
  3. Rejected after Resolve.
  4. Resolved after Reject.
*Resolved more than once new Promise((resolve, reject) => {
resolve('First call');
resolve('Swallowed resolve');
});
*Rejected more than once.new Promise((resolve, reject) => {
reject('First call');
reject('Swallowed reject');
});
*Rejected after Resolvenew Promise((resolve, reject) => {
resolve('First call');
reject('Swallowed reject');
});
*Resolved after Rejectnew Promise((resolve, reject) => {
reject('Swallowed reject');
resolve('First call');
});
process.on('multipleResolves', (type, promise, reason) => {
console.error(type, promise, reason);
});

6. unHandledRejection

new Promise((resolve,reject)=>{
reject("something went wrong")
})
process.on('unhandledRejection',(err)=>{
console.log("Rejection unHandled",err);})

7. uncaughtException

process.on('uncaughtException', (err) => {
console.log("Exception unHandled", err);
})
function fun() {
throw "Something went wrong"}
fun();

8. warning

process.on('warning', (warning) => {
console.warn(warning.name);
console.warn(warning.message);
console.warn(warning.stack);
});

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Abhishek N

Abhishek is a Software developer who is passionate about learning technology related stuff and implementing it in real world.