Dev Helpers
← Back

Subscribe only once

Subscribe only once using Node.js.

Open this example in the interactive library

Source code

import { EventEmitter, once, getEventListeners } from "node:events";
const bus = new EventEmitter();
console.log(
  (() => {
    let count = 0;
    bus.once("tick", () => count++);
    bus.emit("tick");
    bus.emit("tick");
    return count;
  })(),
);

Sample output

1