捕捉 Promise 拒绝
将 async
函数与事件句柄一起使用是有问题的,因为它会在抛出异常的情况下导致未处理的拒绝:
const ee = new EventEmitter();
ee.on('something', async (value) => {
throw new Error('kaboom');
});
EventEmitter
构造函数中的 captureRejections
选项或全局的设置可以改变这种行为,在 Promise
上安装 .then(undefined, handler)
句柄。
此句柄将异常异步地路由到 Symbol.for('nodejs.rejection')
方法(如果有)或 'error'
事件句柄(如果没有)。
const ee1 = new EventEmitter({ captureRejections: true });
ee1.on('something', async (value) => {
throw new Error('kaboom');
});
ee1.on('error', console.log);
const ee2 = new EventEmitter({ captureRejections: true });
ee2.on('something', async (value) => {
throw new Error('kaboom');
});
ee2[Symbol.for('nodejs.rejection')] = console.log;
设置 events.captureRejections = true
将更改 EventEmitter
的所有新实例的默认值。
import { EventEmitter } from 'node:events';
EventEmitter.captureRejections = true;
const ee1 = new EventEmitter();
ee1.on('something', async (value) => {
throw new Error('kaboom');
});
ee1.on('error', console.log);
const events = require('node:events');
events.captureRejections = true;
const ee1 = new events.EventEmitter();
ee1.on('something', async (value) => {
throw new Error('kaboom');
});
ee1.on('error', console.log);
由 captureRejections
行为生成的 'error'
事件没有捕获句柄以避免无限的错误循环:建议不要使用 async
函数作为 'error'
事件句柄。
Using async
functions with event handlers is problematic, because it
can lead to an unhandled rejection in case of a thrown exception:
const ee = new EventEmitter();
ee.on('something', async (value) => {
throw new Error('kaboom');
});
The captureRejections
option in the EventEmitter
constructor or the global
setting change this behavior, installing a .then(undefined, handler)
handler on the Promise
. This handler routes the exception
asynchronously to the Symbol.for('nodejs.rejection')
method
if there is one, or to 'error'
event handler if there is none.
const ee1 = new EventEmitter({ captureRejections: true });
ee1.on('something', async (value) => {
throw new Error('kaboom');
});
ee1.on('error', console.log);
const ee2 = new EventEmitter({ captureRejections: true });
ee2.on('something', async (value) => {
throw new Error('kaboom');
});
ee2[Symbol.for('nodejs.rejection')] = console.log;
Setting events.captureRejections = true
will change the default for all
new instances of EventEmitter
.
import { EventEmitter } from 'node:events';
EventEmitter.captureRejections = true;
const ee1 = new EventEmitter();
ee1.on('something', async (value) => {
throw new Error('kaboom');
});
ee1.on('error', console.log);
const events = require('node:events');
events.captureRejections = true;
const ee1 = new events.EventEmitter();
ee1.on('something', async (value) => {
throw new Error('kaboom');
});
ee1.on('error', console.log);
The 'error'
events that are generated by the captureRejections
behavior
do not have a catch handler to avoid infinite error loops: the
recommendation is to not use async
functions as 'error'
event handlers.