Skip to main content
Deno 2 is finally here 🎉️
Learn more
Module

std/node/events.ts>default#once

Deno standard library
Go to Latest
The Standard Library has been moved to JSR. See the blog post for details.
method default.prototype.once
import { default } from "https://deno.land/std@0.127.0/node/events.ts";

Adds a one-timelistener function for the event named eventName. The next time eventName is triggered, this listener is removed and then invoked.

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. Theemitter.prependOnceListener() method can be used as an alternative to add the event listener to the beginning of the listeners array.

const myEE = new EventEmitter();
myEE.once('foo', () => console.log('a'));
myEE.prependOnceListener('foo', () => console.log('b'));
myEE.emit('foo');
// Prints:
//   b
//   a

Parameters

eventName: string | symbol

The name of the event.

listener: (...args: any[]) => void

The callback function