EventTarget
Node API: Event Target
The EventTarget and Event objects are a Node.js-specific implementation of the EventTarget Web API that are exposed by some Node.js core APIs.
The EventTarget interface is implemented by objects that can receive events and may have listeners for them; events are dispatched via dispatchEvent, and listeners are managed via addEventListener and removeEventListener.
Event targets are implementable from host code or guest code.
Event targets in Node.js-style dispatch form differ slightly from web-standard event targets; see below for more information.
Node.js EventTarget
vs. DOM EventTarget
There are two key differences between the Node.js EventTarget and the EventTarget Web API:
Whereas DOM EventTarget instances may be hierarchical, there is no concept of hierarchy and event propagation in Node.js. That is, an event dispatched to an EventTarget does not propagate through a hierarchy of nested target objects that may each have their own set of handlers for the event.
In the Node.js EventTarget, if an event listener is an async function or returns a
Promise
, and the returnedPromise
rejects, the rejection is automatically captured and handled the same way as a listener that throws synchronously (see EventTarget error handling docs for details).
Inheritors
Functions
Dispatches the event to the list of handlers for Event.type.
Removes the listener
from the list of handlers for event type
.