A utility to promisify event emitters, allowing you to wait for events using promises or async/await.
p-event is a JavaScript library that converts event emitters into promises, allowing developers to wait for events using async/await syntax. It solves the problem of working with callback-based event APIs by providing a promise-based interface that integrates seamlessly with modern asynchronous code patterns. The library supports both Node.js and browser environments and handles single events, multiple events, and async iteration over event streams.
JavaScript and TypeScript developers working with event-driven APIs in Node.js or the browser, especially those looking to simplify asynchronous event handling with promises.
Developers choose p-event because it provides a clean, promise-based abstraction over event emitters, reducing callback hell and improving code readability. Its comprehensive feature set—including filtering, timeouts, abort signals, and TypeScript support—makes it a versatile tool for handling events in async workflows.
Promisify an event by waiting for it to be emitted
Open-Awesome is built by the community, for the community. Submit a project, suggest an awesome list, or help improve the catalog on GitHub.
Converts event emitters to promises, enabling async/await syntax as shown in the before-and-after example, making callback-heavy code more readable and maintainable.
Provides pEventIterator for handling streams of events with options like limit and resolutionEvents, useful for paginated or finite event sequences without manual listener management.
Supports synchronous and asynchronous filter functions to conditionally accept events, demonstrated with examples for value-based validation or API checks.
Integrates with AbortSignal for cancellation, offering a standard, future-proof way to abort event waiting, as highlighted in the migration tips from .cancel().
Due to a TypeScript issue, p-event cannot automatically infer types from EventEmitter subclasses, requiring manual casting with TypedEventEmitter—a workaround that adds boilerplate.
For basic event listening, using p-event introduces a promise layer where direct event listeners might be simpler, more performant, and easier to debug.
Errors in filter functions reject the promise directly, forcing developers to wrap filters in try-catch blocks for graceful handling, which adds complexity.