事件:'multipleResolves'
添加於:v10.12.0自以下版本棄用:v17.6.0
Stability: 0 - 已棄用
參數
type
<string> 分辨率類型。'resolve'
或'reject'
之一。promise
<Promise> 多次解決或拒絕的承諾。value
<any> 在原始解析後,promise 被解析或拒絕的值。
每當 Promise
出現以下任一情況時,都會發出 'multipleResolves'
事件:
- 解決了不止一次。
- 不止一次被拒絕。
- 解決後拒絕。
- 拒絕後解決。
這對於在使用 Promise
構造函數時跟蹤應用程序中的潛在錯誤很有用,因為多個分辨率會被默默吞下。但是,此事件的發生並不一定表示錯誤。例如,
可以觸發 Promise.race()
'multipleResolves'
事件。
由於在上麵的
示例等情況下事件的不可靠性,它已被棄用。Promise.race()
import process from 'node:process'; process.on('multipleResolves', (type, promise, reason) => { console.error(type, promise, reason); setImmediate(() => process.exit(1)); }); async function main() { try { return await new Promise((resolve, reject) => { resolve('First call'); resolve('Swallowed resolve'); reject(new Error('Swallowed reject')); }); } catch { throw new Error('Failed'); } } main().then(console.log); // resolve: Promise { 'First call' } 'Swallowed resolve' // reject: Promise { 'First call' } Error: Swallowed reject // at Promise (*) // at new Promise (<anonymous>) // at main (*) // First call
const process = require('node:process'); process.on('multipleResolves', (type, promise, reason) => { console.error(type, promise, reason); setImmediate(() => process.exit(1)); }); async function main() { try { return await new Promise((resolve, reject) => { resolve('First call'); resolve('Swallowed resolve'); reject(new Error('Swallowed reject')); }); } catch { throw new Error('Failed'); } } main().then(console.log); // resolve: Promise { 'First call' } 'Swallowed resolve' // reject: Promise { 'First call' } Error: Swallowed reject // at Promise (*) // at new Promise (<anonymous>) // at main (*) // First call
相關用法
- Node.js Worker 'message'事件用法及代碼示例
- Node.js tls.Server 'keylog'事件用法及代碼示例
- Node.js http.Server 'clientError'事件用法及代碼示例
- Node.js cluste 'disconnect'事件用法及代碼示例
- Node.js proces 'exit'事件用法及代碼示例
- Node.js stream.Writable 'pipe'事件用法及代碼示例
- Node.js stream.Readable 'end'事件用法及代碼示例
- Node.js cluste 'fork'事件用法及代碼示例
- Node.js stream.Writable 'unpipe'事件用法及代碼示例
- Node.js Http2Session 'remoteSettings'事件用法及代碼示例
- Node.js Worker 'listening'事件用法及代碼示例
- Node.js tls.Server 'resumeSession'事件用法及代碼示例
- Node.js InterfaceConstructor 'pause'事件用法及代碼示例
- Node.js fs.FSWatcher 'change'事件用法及代碼示例
- Node.js stream.Readable 'data'事件用法及代碼示例
- Node.js http.ClientRequest 'connect'事件用法及代碼示例
- Node.js proces 'uncaughtException'事件用法及代碼示例
- Node.js Http2Session 'localSettings'事件用法及代碼示例
- Node.js REPLServer 'exit'事件用法及代碼示例
- Node.js cluste 'online'事件用法及代碼示例
- Node.js tls.TLSSocket 'session'事件用法及代碼示例
- Node.js Worker 'exit'事件用法及代碼示例
- Node.js cluste 'exit'事件用法及代碼示例
- Node.js Http2Stream 'trailers'事件用法及代碼示例
- Node.js MessagePort 'close'事件用法及代碼示例
注:本文由純淨天空篩選整理自nodejs.org大神的英文原創作品 'multipleResolves'事件。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。