Events
EventEmitter.once()
Registers a handler that removes itself before its first invocation. Tested on Node.js 22.16.0. Distinct from events.once(), which produces a Promise. Register before the event can occur.
Formal Syntax & Structure
nodejs syntaxnodejs
emitter.once(eventName, listener)Parameters & Attributes
| Name | Type | Description |
|---|---|---|
| eventName | string | symbol | Name of the event. |
| listener | Function | Handler function. |
Return Value
EventEmitter
The same emitter.
Working Example
Example Snippetnodejs
import { EventEmitter } from 'node:events';const bus=new EventEmitter();bus.once('ready',()=>console.log('ready'));bus.emit('ready');bus.emit('ready');