Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

async-eventemitter

Package Overview
Dependencies
Maintainers
1
Versions
6
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

async-eventemitter

Just like EventEmitter, but with support for callbacks and interuption of the listener-chain

  • 0.1.0
  • npm
  • Socket score

Version published
Weekly downloads
100K
decreased by-34.05%
Maintainers
1
Weekly downloads
 
Created

What is async-eventemitter?

The async-eventemitter npm package is an extension of the Node.js EventEmitter that supports asynchronous event handlers. It allows you to handle events with asynchronous functions, making it easier to work with promises and async/await in event-driven programming.

What are async-eventemitter's main functionalities?

Basic Asynchronous Event Handling

This feature allows you to register an asynchronous event handler using the `on` method. The event handler can use async/await to handle asynchronous operations.

const AsyncEventEmitter = require('async-eventemitter');
const emitter = new AsyncEventEmitter();

emitter.on('event', async (data) => {
  await new Promise(resolve => setTimeout(resolve, 1000));
  console.log('Event handled with data:', data);
});

emitter.emit('event', 'some data');

Handling Multiple Asynchronous Listeners

This feature demonstrates how multiple asynchronous listeners can be registered for the same event. Each listener will handle the event asynchronously and independently.

const AsyncEventEmitter = require('async-eventemitter');
const emitter = new AsyncEventEmitter();

emitter.on('event', async (data) => {
  await new Promise(resolve => setTimeout(resolve, 1000));
  console.log('First listener handled with data:', data);
});

emitter.on('event', async (data) => {
  await new Promise(resolve => setTimeout(resolve, 500));
  console.log('Second listener handled with data:', data);
});

emitter.emit('event', 'some data');

Waiting for All Listeners to Complete

This feature shows how to wait for all asynchronous listeners to complete before proceeding. The `emit` method returns a promise that resolves when all listeners have finished handling the event.

const AsyncEventEmitter = require('async-eventemitter');
const emitter = new AsyncEventEmitter();

emitter.on('event', async (data) => {
  await new Promise(resolve => setTimeout(resolve, 1000));
  console.log('First listener handled with data:', data);
});

emitter.on('event', async (data) => {
  await new Promise(resolve => setTimeout(resolve, 500));
  console.log('Second listener handled with data:', data);
});

(async () => {
  await emitter.emit('event', 'some data');
  console.log('All listeners have completed');
})();

Other packages similar to async-eventemitter

Keywords

FAQs

Package last updated on 10 Nov 2013

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

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc