捕获对 promise 的拒绝
【Capture rejections of promises】
在事件处理程序中使用 async 函数是有问题的,因为在抛出异常时可能会导致未处理的拒绝:
【Using async functions with event handlers is problematic, because it
can lead to an unhandled rejection in case of a thrown exception:】
import { EventEmitter } from 'node:events';
const ee = new EventEmitter();
ee.on('something', async (value) => {
throw new Error('kaboom');
});const EventEmitter = require('node:events');
const ee = new EventEmitter();
ee.on('something', async (value) => {
throw new Error('kaboom');
});EventEmitter 构造函数中的 captureRejections 选项或全局设置会改变这种行为,在 Promise 上安装一个 .then(undefined, handler) 处理程序。这个处理程序会将异常异步地路由到 Symbol.for('nodejs.rejection') 方法(如果存在),如果不存在,则路由到 'error' 事件处理程序。
【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.】
import { EventEmitter } from 'node:events';
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;const EventEmitter = require('node:events');
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 设置为 true 将更改所有新的 EventEmitter 实例的默认值。
【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);由 captureRejections 行为生成的 'error' 事件没有捕获处理程序,以避免无限错误循环:建议 不要将 async 函数用作 'error' 事件处理程序。
【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.】