emitter[Symbol.for('nodejs.rejection')](err, eventName[, ...args])


稳定性: 1 - captureRejections 是实验的。

Symbol.for('nodejs.rejection') 方法被调用,以防在触发事件时发生 promise 拒绝,并且在触发器上启用了 captureRejections。 可以使用 events.captureRejectionSymbol 代替 Symbol.for('nodejs.rejection')

const { EventEmitter, captureRejectionSymbol } = require('events');

class MyClass extends EventEmitter {
  constructor() {
    super({ captureRejections: true });
  }

  [captureRejectionSymbol](err, event, ...args) {
    console.log('rejection happened for', event, 'with', err, ...args);
    this.destroy(err);
  }

  destroy(err) {
    // 把这里的资源卸除。
  }
}

Stability: 1 - captureRejections is experimental.

The Symbol.for('nodejs.rejection') method is called in case a promise rejection happens when emitting an event and captureRejections is enabled on the emitter. It is possible to use events.captureRejectionSymbol in place of Symbol.for('nodejs.rejection').

const { EventEmitter, captureRejectionSymbol } = require('events');

class MyClass extends EventEmitter {
  constructor() {
    super({ captureRejections: true });
  }

  [captureRejectionSymbol](err, event, ...args) {
    console.log('rejection happened for', event, 'with', err, ...args);
    this.destroy(err);
  }

  destroy(err) {
    // Tear the resource down here.
  }
}