emitter.once() / events.once()
Registers a one-time listener, or awaits a single emission as a Promise.
Syntax
emitter.once(name, listener) / await once(emitter, name) Parameters
| Name | Type | Required | Description |
|---|---|---|---|
eventName | string | symbol | Yes | The event to listen for once. |
listener | function | No | The callback (method form). Removed automatically after one call. |
Returns
EventEmitter | Promise<any[]> — The emitter, or a Promise resolving with the emitted args.
Examples
import { EventEmitter } from 'node:events';
const ee = new EventEmitter();
ee.once('init', () => console.log('runs once'));
ee.emit('init');
ee.emit('init');
Output
runs once
import { once } from 'node:events';
import { createServer } from 'node:http';
const server = createServer().listen(3000);
await once(server, 'listening');
console.log('server is up');
Output
server is up
Notes
The standalone `events.once()` is ideal for awaiting startup events.
It also rejects if the emitter emits `'error'` first, so wrap it in
try/catch. The listener form auto-removes after the first emission.