• emitter {EventEmitter}
    • eventName {string|symbol} The name of the event being listened for
    • Returns: {AsyncIterator} that iterates eventName events emitted by the emitter
    1. const { on, EventEmitter } = require('events');
    2. (async () => {
    3. const ee = new EventEmitter();
    4. // Emit later on
    5. process.nextTick(() => {
    6. ee.emit('foo', 'bar');
    7. ee.emit('foo', 42);
    8. });
    9. for await (const event of on(ee, 'foo')) {
    10. // The execution of this inner block is synchronous and it
    11. // processes one event at a time (even with await). Do not use
    12. // if concurrent execution is required.
    13. console.log(event); // prints ['bar'] [42]
    14. }
    15. // Unreachable here
    16. })();

    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.