當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


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。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。