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

NameTypeDescription
eventNamestring | symbolName of the event.
listenerFunctionHandler 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');
Node.js Reference