Class AsyncEventEmitter<T>

An event emitter that has better support async methods giving the emitter the option to execute event handlers async in sequence instead of in parallel.

All handlers can be removed using

If async event handlers throw an error when hideExceptions is set to false the emitter will receive the error.

Example

// Prints "Hello world" after 1 second
// before "End!"
const emitter = new AsyncEventEmitter();
emitter.on('hello', async (params) => {
await new Promise(resolve => setTimeout(resolve, 1000));
console.log('Hello', params);
});
// pass `{async: true}` to not await the handler completion
// which will print "End!" before "Hello world"
await emitter.emit('hello', 'world');
console.log('End!');

Type Parameters

  • T extends EventMap = any

Hierarchy

Constructors

Properties

listeners: Map<string, {
    callback: EventReceiver<any>;
} & EventListenerOptions> = ...

Methods

  • Emit an event and await the event completion

    Type Parameters

    • K extends string

    Parameters

    • event: K

      event type that is emptied

    • params: T[K]

      parameters matching the event type

    • Optional options: EventEmitOptions

    Returns Promise<boolean>

  • Register an event listener to trigger on an event.

    Type Parameters

    • K extends string

    Parameters

    • event: K
    • listener: EventReceiver<T[K]>

      Listener to register

    Returns EventToken

  • Handles the error that occurs when emitting a callback event.

    Parameters

    • err: unknown

      The error that occurred.

    • event: string

      The name of the event.

    • failedCallback: EventReceiver<any>

      The failed callback event receiver.

    Returns void

  • Register an event listener to trigger once on event.

    Type Parameters

    • K extends string

    Parameters

    • event: K
    • listener: EventReceiver<T[K]>

      Listener to register

    Returns EventToken

  • Register an event listener to trigger on an event.

    Type Parameters

    • K extends string

    Parameters

    • event: string
    • listener: EventReceiver<T[K]>

      Listener to register

    • options: EventListenerOptions

    Returns EventToken

  • Removes all listeners, or those of the specified event.

    It is bad practice to remove listeners added elsewhere in the code, particularly when the EventEmitter instance was created by some other component or module (e.g. sockets or file streams).

    Returns a reference to the EventEmitter, so that calls can be chained.

    Type Parameters

    • K extends string

    Parameters

    • Optional event: K

      event type to remove listeners for if not specified all listeners are removed

    Returns void

  • Removes the specified listener from the listener array for the event named event.

    removeListener() will remove, at most, one instance of a listener from the listener array. If any single listener has been added multiple times to the listener array for the specified eventName, then removeListener() must be called multiple times to remove each instance.

    Type Parameters

    • K extends string

    Parameters

    • event: K

      event type to remove listener from

    • listener: EventReceiver<T[K]>

      listener to remove

    Returns AsyncEventEmitter<T>