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

p-event

Package Overview
Dependencies
Maintainers
1
Versions
17
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

p-event

Promisify an event by waiting for it to be emitted

  • 1.3.0
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
5.2M
decreased by-8.33%
Maintainers
1
Weekly downloads
 
Created

What is p-event?

The p-event package is a utility for converting event emitters into promises. It allows you to wait for an event to be emitted, making it easier to work with asynchronous event-driven code.

What are p-event's main functionalities?

Wait for a single event

This feature allows you to wait for a single event to be emitted. The promise resolves with the event data when the event is emitted.

const pEvent = require('p-event');
const EventEmitter = require('events');
const emitter = new EventEmitter();
(async () => {
  const eventData = await pEvent(emitter, 'data');
  console.log(eventData); // Logs the data emitted
})();
emitter.emit('data', 'Hello, world!');

Wait for multiple events

This feature allows you to wait for multiple events to be emitted. The promise resolves when all specified events have been emitted.

const pEvent = require('p-event');
const EventEmitter = require('events');
const emitter = new EventEmitter();
(async () => {
  const [data1, data2] = await Promise.all([
    pEvent(emitter, 'data1'),
    pEvent(emitter, 'data2')
  ]);
  console.log(data1, data2); // Logs the data emitted by both events
})();
emitter.emit('data1', 'First event');
emitter.emit('data2', 'Second event');

Timeout for events

This feature allows you to specify a timeout for waiting for an event. If the event is not emitted within the specified time, the promise is rejected.

const pEvent = require('p-event');
const EventEmitter = require('events');
const emitter = new EventEmitter();
(async () => {
  try {
    const eventData = await pEvent(emitter, 'data', {timeout: 1000});
    console.log(eventData);
  } catch (error) {
    console.error('Event did not occur within 1 second');
  }
})();

Filter events

This feature allows you to filter events based on a condition. The promise resolves only when an event that matches the filter condition is emitted.

const pEvent = require('p-event');
const EventEmitter = require('events');
const emitter = new EventEmitter();
(async () => {
  const eventData = await pEvent(emitter, 'data', {
    filter: data => data === 'specific data'
  });
  console.log(eventData); // Logs 'specific data'
})();
emitter.emit('data', 'other data');
emitter.emit('data', 'specific data');

Other packages similar to p-event

Keywords

FAQs

Package last updated on 21 Jun 2017

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