Events
Stability: 2 - Stable
Function objects (“listeners”) to be called.
For instance: a net.Server object emits an event each time a peer connects to it; a fs.ReadStream emits an event when the file is opened; a stream emits an event whenever data is available to be read.
All objects that emit events are instances of the EventEmitter class. These objects expose an eventEmitter.on() function that allows one or more functions to be attached to named events emitted by the object.
Passing Arguments and this to Listeners
TheeventEmitter.emit() method allows an arbitrary set of arguments to be passed to the listener functions. When an ordinary listener function is called, the standard this keyword is intentionally set to reference the EventEmitter instance to which the listener is attached.
this keyword will no longer reference the EventEmitter instance:
Asynchronous vs. Synchronous
TheEventEmitter calls all listeners synchronously in the order in which they were registered. This ensures the proper sequencing of events and helps avoid race conditions and logic errors. When appropriate, listener functions can switch to an asynchronous mode of operation using setImmediate() or process.nextTick():
Handling Events Only Once
When a listener is registered using theeventEmitter.on() method, that listener is invoked every time the named event is emitted.
eventEmitter.once() method, it is possible to register a listener that is called at most once for a particular event:
Error Events
When an error occurs within anEventEmitter instance, the typical action is for an ‘error’ event to be emitted. These are treated as special cases within Node.js.
If an EventEmitter does not have at least one listener registered for the ‘error’ event, and an ‘error’ event is emitted, the error is thrown, a stack trace is printed, and the Node.js process exits.
Class: EventEmitter
TheEventEmitter class is defined and exposed by the node:events module.
EventEmitters emit the event ‘newListener’ when new listeners are added and ‘removeListener’ when existing listeners are removed.
new EventEmitter([options])
Event: ‘newListener’
Added in: v0.1.26
The name of the event being listened for
The event handler function
EventEmitter instance will emit its own ‘newListener’ event before a listener is added to its internal array of listeners.
Event: ‘removeListener’
Added in: v0.9.3
The event name
The event handler function
listener is removed.
emitter.addListener(eventName, listener)
Added in: v0.1.26
The name of the event
The callback function
emitter.on(eventName, listener).
emitter.emit(eventName[, …args])
Added in: v0.1.26
The name of the event
Arguments to pass to the listeners
Returns true if the event had listeners, false otherwise.
eventName, in the order they were registered, passing the supplied arguments to each.
emitter.eventNames()
Added in: v6.0.0
Returns an array listing the events for which the emitter has registered listeners.
emitter.getMaxListeners()
Added in: v1.0.0
Returns the current max listener value for the EventEmitter.
EventEmitter which is either set by emitter.setMaxListeners(n) or defaults to EventEmitter.defaultMaxListeners.
emitter.listenerCount(eventName[, listener])
Added in: v3.2.0
The name of the event being listened for
The event handler function
Returns the number of listeners listening for the event named eventName.
emitter.listeners(eventName)
Added in: v0.1.26
The name of the event
Returns a copy of the array of listeners for the event named eventName.
emitter.off(eventName, listener)
Added in: v10.0.0
The name of the event
The callback function
emitter.removeListener().
emitter.on(eventName, listener)
Added in: v0.1.101
The name of the event
The callback function
listener function to the end of the listeners array for the event named eventName. No checks are made to see if the listener has already been added. Multiple calls passing the same combination of eventName and listener will result in the listener being added, and called, multiple times.
EventEmitter, so that calls can be chained.
emitter.once(eventName, listener)
Added in: v0.3.0
The name of the event
The callback function
listener function for the event named eventName. The next time eventName is triggered, this listener is removed and then invoked.
EventEmitter, so that calls can be chained.
emitter.prependListener(eventName, listener)
Added in: v6.0.0
The name of the event
The callback function
listener function to the beginning of the listeners array for the event named eventName. No checks are made to see if the listener has already been added.
EventEmitter, so that calls can be chained.
emitter.prependOnceListener(eventName, listener)
Added in: v6.0.0
The name of the event
The callback function
listener function for the event named eventName to the beginning of the listeners array.
emitter.removeAllListeners([eventName])
Added in: v0.1.26
The name of the event
eventName.
Returns a reference to the EventEmitter, so that calls can be chained.
emitter.removeListener(eventName, listener)
Added in: v0.1.26
The name of the event
The callback function
listener from the listener array for the event named eventName.
emitter.setMaxListeners(n)
Added in: v0.3.5
The new maximum number of listeners
emitter.setMaxListeners() method allows the limit to be modified for this specific EventEmitter instance. The value can be set to Infinity (or 0) to indicate an unlimited number of listeners.
Returns a reference to the EventEmitter, so that calls can be chained.
emitter.rawListeners(eventName)
Added in: v9.4.0
The name of the event
Returns a copy of the array of listeners for the event named eventName, including any wrappers (such as those created by .once()).
events.once(emitter, name[, options])
Added in: v11.13.0, v10.16.0
The event emitter
The name of the event being listened for
Promise that is fulfilled when the EventEmitter emits the given event or that is rejected if the EventEmitter emits ‘error’ while waiting.
events.on(emitter, eventName[, options])
Added in: v13.6.0, v12.16.0
The event emitter
The name of the event being listened for
Returns an AsyncIterator that iterates eventName events emitted by the emitter.
EventEmitter.defaultMaxListeners
By default, a maximum of 10 listeners can be registered for any single event. This limit can be changed for individual EventEmitter instances using the emitter.setMaxListeners(n) method. To change the default for all EventEmitter instances, the EventEmitter.defaultMaxListeners property can be used.