Skip to content

addListener()

Attaches a listener function to an event, allowing it to be called when the event is emitted.

Example

typescript
instance.addListener("foo", () => {
  /* [...] */
});

Also read about providing a context to control the value of this inside the listener function.

Parameters

ParameterTypeDefaultDescription
eventstring | symbolThe event name. Expects an ESMitterEventName.
fnunknownThe function that will be called then the event is emitted.
contextunknownundefinedThe context to invoke the listener with.
oncebooleanfalseSpecify if the listener is a one-time listener.

Returns

TypeDescription
ESMitterThe ESMitter instance, allowing for method chaining.

on()

The on() method is almost an alias of addListener(), the only exception being the missing once parameter. For this, use once() instead.