Skip to main content
EventEmitter.once - node__events.d.ts - Node documentation
method EventEmitter.once

Usage in Deno

```typescript import { type EventEmitter } from "node:node__events.d.ts"; ```
EventEmitter.once<K>(
eventName: Key<K, T>,
listener: Listener1<K, T>,
): this
Adds a **one-time** `listener` function for the event named `eventName`. The next time `eventName` is triggered, this listener is removed and then invoked. ```js server.once('connection', (stream) => { console.log('Ah, we have our first user!'); }); ``` Returns a reference to the `EventEmitter`, so that calls can be chained. By default, event listeners are invoked in the order they are added. The `emitter.prependOnceListener()` method can be used as an alternative to add the event listener to the beginning of the listeners array. ```js import { EventEmitter } from 'node:events'; const myEE = new EventEmitter(); myEE.once('foo', () => console.log('a')); myEE.prependOnceListener('foo', () => console.log('b')); myEE.emit('foo'); // Prints: // b // a ```

Type Parameters

K

Parameters

eventName: Key<K, T>
The name of the event.
listener: Listener1<K, T>
The callback function

Return Type

this