What is @vladfrangu/async_event_emitter?
@vladfrangu/async_event_emitter is an npm package that provides an asynchronous event emitter. It allows you to handle events in an asynchronous manner, making it suitable for scenarios where you need to perform asynchronous operations in response to events.
What are @vladfrangu/async_event_emitter's main functionalities?
Basic Event Emission
This feature allows you to emit events and handle them asynchronously. The event handler can perform asynchronous operations, such as fetching data from a database or an API.
const { AsyncEventEmitter } = require('@vladfrangu/async_event_emitter');
const emitter = new AsyncEventEmitter();
emitter.on('event', async (data) => {
console.log('Event received:', data);
});
(async () => {
await emitter.emit('event', { key: 'value' });
})();
Handling Multiple Listeners
This feature allows you to register multiple listeners for the same event. All listeners will be called asynchronously when the event is emitted.
const { AsyncEventEmitter } = require('@vladfrangu/async_event_emitter');
const emitter = new AsyncEventEmitter();
emitter.on('event', async (data) => {
console.log('First listener:', data);
});
emitter.on('event', async (data) => {
console.log('Second listener:', data);
});
(async () => {
await emitter.emit('event', { key: 'value' });
})();
Removing Listeners
This feature allows you to remove specific listeners from an event. This is useful for cleaning up resources or stopping certain operations when they are no longer needed.
const { AsyncEventEmitter } = require('@vladfrangu/async_event_emitter');
const emitter = new AsyncEventEmitter();
const listener = async (data) => {
console.log('Event received:', data);
};
emitter.on('event', listener);
(async () => {
await emitter.emit('event', { key: 'value' });
emitter.off('event', listener);
await emitter.emit('event', { key: 'value' }); // This will not trigger the listener
})();
Other packages similar to @vladfrangu/async_event_emitter
events
The 'events' package is the standard Node.js event emitter. It provides synchronous event handling and is widely used in the Node.js ecosystem. Unlike @vladfrangu/async_event_emitter, it does not natively support asynchronous event handlers.
eventemitter3
The 'eventemitter3' package is a lightweight and fast event emitter for Node.js and the browser. It supports both synchronous and asynchronous event handling, but it requires manual handling of asynchronous operations, unlike @vladfrangu/async_event_emitter which is designed specifically for asynchronous events.
async-eventemitter
The 'async-eventemitter' package is similar to @vladfrangu/async_event_emitter in that it provides asynchronous event handling. It allows you to emit events and handle them with asynchronous functions, making it a direct alternative to @vladfrangu/async_event_emitter.
@vladfrangu/async_event_emitter
Simple to use event emitter implementation with async support in mind.
Description
A very small, lightweight and simple re-implementation of event emitters, with support for async event handlers in mind.
Note: @vladfrangu/async_event_emitter
requires Node.js v14 or higher to work, or a browser that has async/await support.
Features
- TypeScript friendly
- Offers CJS, ESM and UMD builds
- Consistent interface with what is expected from an event emitter
- Simple handling of asynchronous event handlers to allow waiting for their execution to finish if you want to exit the process
Buy me some doughnuts
Most of my projects are and always will be open source, even if I don't get donations. That being said, I know there are amazing people who may still want to donate just to show their appreciation. Thank you very much in advance!
I accept donations through Ko-fi, PayPal, Patreon and GitHub Sponsorships. You can use the buttons below to donate through your method of choice.
Contributors ✨
Thanks goes to these wonderful people (emoji key):
This project follows the all-contributors specification. Contributions of any kind welcome!