events.on(emitter, eventName[, options])


  • emitter <EventEmitter>
  • eventName <string> | <symbol> 要监听的事件名称
  • options <Object>
    • signal <AbortSignal> 可用于取消等待的事件。
    • close - <string[]> 将结束迭代的事件名称。
    • highWaterMark - <integer> 默认值: Number.MAX_SAFE_INTEGER 高水位值。每当缓冲的事件数量超过此值时,触发器将被暂停。仅支持实现了 pause()resume() 方法的触发器。
    • lowWaterMark - <integer> 默认值: 1 低水位值。每当缓冲的事件数量低于此值时,触发器将恢复。仅支持实现了 pause()resume() 方法的触发器。
  • 返回: <AsyncIterator>,用于迭代 emitter 发出的 eventName 事件
import { on, EventEmitter } from 'node:events';
import process from 'node:process';

const ee = new EventEmitter();

// Emit later on
process.nextTick(() => {
  ee.emit('foo', 'bar');
  ee.emit('foo', 42);
});

for await (const event of on(ee, 'foo')) {
  // The execution of this inner block is synchronous and it
  // processes one event at a time (even with await). Do not use
  // if concurrent execution is required.
  console.log(event); // prints ['bar'] [42]
}
// Unreachable hereconst { on, EventEmitter } = require('node:events');

(async () => {
  const ee = new EventEmitter();

  // Emit later on
  process.nextTick(() => {
    ee.emit('foo', 'bar');
    ee.emit('foo', 42);
  });

  for await (const event of on(ee, 'foo')) {
    // The execution of this inner block is synchronous and it
    // processes one event at a time (even with await). Do not use
    // if concurrent execution is required.
    console.log(event); // prints ['bar'] [42]
  }
  // Unreachable here
})();

返回一个 AsyncIterator,用于迭代 eventName 事件。如果 EventEmitter 发出 'error',它将抛出异常。退出循环时会移除所有监听器。每次迭代返回的 value 是一个由触发事件的参数组成的数组。

【Returns an AsyncIterator that iterates eventName events. It will throw if the EventEmitter emits 'error'. It removes all listeners when exiting the loop. The value returned by each iteration is an array composed of the emitted event arguments.】

可以使用 <AbortSignal> 来取消等待事件:

【An <AbortSignal> can be used to cancel waiting on events:】

import { on, EventEmitter } from 'node:events';
import process from 'node:process';

const ac = new AbortController();

(async () => {
  const ee = new EventEmitter();

  // Emit later on
  process.nextTick(() => {
    ee.emit('foo', 'bar');
    ee.emit('foo', 42);
  });

  for await (const event of on(ee, 'foo', { signal: ac.signal })) {
    // The execution of this inner block is synchronous and it
    // processes one event at a time (even with await). Do not use
    // if concurrent execution is required.
    console.log(event); // prints ['bar'] [42]
  }
  // Unreachable here
})();

process.nextTick(() => ac.abort());const { on, EventEmitter } = require('node:events');

const ac = new AbortController();

(async () => {
  const ee = new EventEmitter();

  // Emit later on
  process.nextTick(() => {
    ee.emit('foo', 'bar');
    ee.emit('foo', 42);
  });

  for await (const event of on(ee, 'foo', { signal: ac.signal })) {
    // The execution of this inner block is synchronous and it
    // processes one event at a time (even with await). Do not use
    // if concurrent execution is required.
    console.log(event); // prints ['bar'] [42]
  }
  // Unreachable here
})();

process.nextTick(() => ac.abort());