当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Node.js Process multipleResolves用法及代码示例


“ multipleResolves”是流程模块中Process类的事件,只要Promise达到以下任一状态,就会发出该事件:

  • 解决了不止一次。
  • 被拒绝多次。
  • 解决后被拒绝。
  • 拒绝后解决。

用法:

Event:'multipleResolves'

参数:此事件不接受任何参数作为参数。

返回值:此事件只返回回调函数以进行进一步的操作。



范例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("Promsie:", 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();

使用以下命令运行index.js文件:

node index.js

输出:

Type:resolve
Promsie:Promise { ‘Resolve Call One’ }
Reason:Resolve Call Two
Type:resolve
Promsie:Promise { ‘Resolve Call One’ }
Reason:Resolve Call Three
Type:reject
Promsie: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

范例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("Promsie:", 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();

使用以下命令运行index.js文件:

node index.js

输出:

Type:reject
Promsie: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

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

相关用法


注:本文由纯净天空筛选整理自RohitPrasad3大神的英文原创作品 Node.js Process multipleResolves Event。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。