Socket
Book a DemoInstallSign in
Socket

@humanwhocodes/async-event-emitter

Package Overview
Dependencies
Maintainers
1
Versions
2
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@humanwhocodes/async-event-emitter

Async event emitter

latest
Source
npmnpm
Version
1.0.1
Version published
Maintainers
1
Created
Source

AsyncEventEmitter

by Nicholas C. Zakas

If you find this useful, please consider supporting my work with a donation.

Description

A utility for emitting events and responding with asynchronous functions.

Usage

Node.js

Install using npm or yarn:

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";

Deno

Import into your Deno project:

import { AsyncEventEmitter } from "https://cdn.skypack.dev/@humanwhocodes/async-event-emitter?dts";

Bun

Install using this command:

bun add @humanwhocodes/async-event-emitter

Import into your Bun project:

import { AsyncEventEmitter } from "@humanwhocodes/async-event-emitter";

Browser

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";

API

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);   // []

Developer Setup

  • Fork the repository
  • Clone your fork
  • Run npm install to setup dependencies
  • Run npm test to run tests

License

Apache 2.0

Keywords

events

FAQs

Package last updated on 10 Aug 2022

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts