Open In App

Node.js Process multipleResolves Event

Last Updated : 19 Apr, 2022
Comments
Improve
Suggest changes
Like Article
Like
Report

The 'multipleResolves' is an event of class Process within process module which is emitted whenever a Promise has been either:

  • Resolved more than once.
  • Rejected more than once.
  • Rejected after resolve.
  • Resolved after reject.

Syntax:

Event: 'multipleResolves'

Parameters: This event does not accept any argument as the parameter.

Return Value: This event returns nothing but a callback function for further operation.

 

Example 1: 

index.js
// Node.js program to demonstrate the  
// Process 'multipleResolves' Event

// Importing process module
const process = require('process');

// Independent Block which will execute
setTimeout(() => {
   console.log('\n')
   console.log('Greetings from GeeksforGeeks');
}, 1000);

// Event 'multipleResolves' 
process.on('multipleResolves', (type, promise, reason) => {

   // Displaying the error 
   console.log("Type: ", type);
   console.log("Promise: ", promise);
   console.log("Reason: ", reason);
});

const myFunction = async () => {
   const data = await new Promise((resolve, reject) => {

      // Calling reject after multiple resolve
      resolve('Resolve Call One');
      resolve('Resolve Call Two');
      resolve('Resolve Call Three');
      reject(new Error('Reject Error Called'));
   });
   return data
}

// Calling our function
myFunction().then();

Run the index.js file using the following command:

node index.js

Output:

Type: resolve Promise: Promise { 'Resolve Call One' } Reason: Resolve Call Two Type: resolve Promise: Promise { 'Resolve Call One' } Reason: Resolve Call Three Type: reject Promise: Promise { 'Resolve Call One' } Reason: Error: Reject Error Called at data (C:\Users\Lenovo\Downloads\GeeksforGeeks\Node JS\index.js:27:14) at new Promise (<anonymous>) at myFunction (C:\Users\Lenovo\Downloads\GeeksforGeeks\Node JS\index.js:22:23) at Object.<anonymous> (C:\Users\Lenovo\Downloads\GeeksforGeeks\Node JS\index.js:33:1) at Module._compile (internal/modules/cjs/loader.js:1138:30) at Object.Module._extensions..js (internal/modules/cjs/loader.js:1158:10) at Module.load (internal/modules/cjs/loader.js:986:32) at Function.Module._load (internal/modules/cjs/loader.js:879:14) at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:71:12) at internal/main/run_main_module.js:17:47 Greetings from GeeksforGeeks

Example 2: 

index.js
// Node.js program to demonstrate the  
// Process 'multipleResolves' Event

// Importing process module
const process = require('process');

// Event 'multipleResolves' 
process.on('multipleResolves', (type, promise, reason) => {

   // Displaying the error 
   console.log("Type: ", type);
   console.log("Promise: ", promise);
   console.log("Reason: ", reason);
});

const myFunction = async () => {
   const data = await new Promise((resolve, reject) => {

      // Calling reject after resolve
      resolve('Single Resolve call');
      reject(new Error('Custom Reason'));
   });
   return data
}

// Calling our function
myFunction().then();

Run the index.js file using the following command:

node index.js

Output:

Type: reject Promise: Promise { 'Single Resolve call } Reason: Error: Custom Reason at data (C:\Users\Lenovo\Downloads\GeeksforGeeks\Node JS\index.js:19:14) at new Promise (<anonymous>) at myFunction (C:\Users\Lenovo\Downloads\GeeksforGeeks\Node JS\index.js:16:23) at Object.<anonymous> (C:\Users\Lenovo\Downloads\GeeksforGeeks\Node JS\index.js:25:1) at Module._compile (internal/modules/cjs/loader.js:1138:30) at Object.Module._extensions..js (internal/modules/cjs/loader.js:1158:10) at Module.load (internal/modules/cjs/loader.js:986:32) at Function.Module._load (internal/modules/cjs/loader.js:879:14) at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:71:12) at internal/main/run_main_module.js:17:47

Reference: https://nodejs.org/dist/latest-v16.x/docs/api/process.html#process_event_multipleresolves


Explore