Stability: 1 - captureRejections is experimental.

    Using async functions with event handlers is problematic, because it can lead to an unhandled rejection in case of a thrown exception:

    1. const ee = new EventEmitter();
    2. ee.on('something', async (value) => {
    3. throw new Error('kaboom');
    4. });

    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')][rejection] method if there is one, or to ['error'][error] event handler if there is none.

    1. const ee1 = new EventEmitter({ captureRejections: true });
    2. ee1.on('something', async (value) => {
    3. throw new Error('kaboom');
    4. });
    5. ee1.on('error', console.log);
    6. const ee2 = new EventEmitter({ captureRejections: true });
    7. ee2.on('something', async (value) => {
    8. throw new Error('kaboom');
    9. });
    10. ee2[Symbol.for('nodejs.rejection')] = console.log;

    Setting EventEmitter.captureRejections = true will change the default for all new instances of EventEmitter.

    1. EventEmitter.captureRejections = true;
    2. const ee1 = new EventEmitter();
    3. ee1.on('something', async (value) => {
    4. throw new Error('kaboom');
    5. });
    6. 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.