events module.
This module, in particular, offers the EventEmitter class, which we’ll use to handle our events.
You initialize that using:
- ESM
- CJS
on and emit methods.
emitis used to trigger an eventonis used to add a callback function that’s going to be executed when the event is triggered
Emitting and listening to events
For example, let’s create astart event, and as a matter of providing a sample, we react to that by just logging to the console:
Passing arguments to event handlers
You can pass arguments to the event handler by passing them as additional arguments toemit():
Additional EventEmitter methods
TheEventEmitter object also exposes several other methods to interact with events:
once(): add a one-time listenerremoveListener()/off(): remove an event listener from an eventremoveAllListeners(): remove all listeners for an event