If you find this useful, please consider supporting my work with a donation.
A utility for emitting events and responding with asynchronous functions.
npm install @humanwhocodes/async-event-emitter
# or
yarn add @humanwhocodes/async-event-emitter
Import into your Node.js project:
// CommonJS
const { AsyncEventEmitter } = require("@humanwhocodes/async-event-emitter");
// ESM
import { AsyncEventEmitter } from "@humanwhocodes/async-event-emitter";
Import into your Deno project:
import { AsyncEventEmitter } from "https://cdn.skypack.dev/@humanwhocodes/async-event-emitter?dts";
Install using this command:
bun add @humanwhocodes/async-event-emitter
Import into your Bun project:
import { AsyncEventEmitter } from "@humanwhocodes/async-event-emitter";
It's recommended to import the minified version to save bandwidth:
import { AsyncEventEmitter } from "https://cdn.skypack.dev/@humanwhocodes/async-event-emitter?min";
However, you can also import the unminified version for debugging purposes:
import { AsyncEventEmitter } from "https://cdn.skypack.dev/@humanwhocodes/async-event-emitter";
After importing, create a new instance of AsyncEventEmitter
to start emitting events:
const emitter = new AsyncEventEmitter();
// add some event handlers - functions can be async or not
emitter.on("foo", async () => "hello!");
emitter.on("foo", () => "goodbye!");
// emit an event
const results = await emitter.emit("foo");
console.log(results); // ["hello!", "goodbye!"]
// you can also pass arguments
emitter.on("exclaim", suffix => "hello" + suffix);
emitter.on("exclaim", suffix => "goodbye" + suffix);
const results = await emitter.emit("exclaim", "!");
console.log(results); // ["hello!", "goodbye!"]
// get the number of handlers for an event
const count = emitter.listenerCount("exclaim");
console.log(count); // 2
// remove any unwanted handlers
const handler = () => {};
emitter.on("bar", handler);
emitter.off("bar", handler);
const results = await emitter.emit("bar");
console.log(results); // []
- Fork the repository
- Clone your fork
- Run
npm install
to setup dependencies - Run
npm test
to run tests
Apache 2.0