npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

wait-for-event-promise

v1.1.0

Published

function that returns a promise in anticipation of an event

Downloads

1,079

Readme

wait-for-event-promise

A simple module that exposes a function used to wait for specific events from an emitter.

Installation

npm install wait-for-event-promise

Usage

waitForEvent(emitter: EventEmitter, eventName: String [, filter: Function, options: Object ])

A minimal example:

const waitForEvent = require('wait-for-event-promise');
const EventEmitter = require('events')

(async function () {
  // create event emitter
  const emitter = new EventEmitter();

  // create a promise for wait for a 'hello' event
  const helloPromise = waitForEvent(emitter, 'hello');

  // emit a hello event
  emitter.emit('hello', {
    message: 'world'
  });

  // promise will resolve with the data that was emit
  const event = await helloPromise; // { message: 'world' }
  console.log(event.message); // outputs: 'world'
})();

A function can be passed into to filter out events that you don't care about.

const emitter = new EventEmitter();

const helloPromise = waitForEvent(emitter, 'hello', (event) => {
  return event === 'world';
});

emitter.emit('hello', 'bob');
// we don't care about bob, so helloPromise has not resolved yet

emitter.emit('hello', 'world');
// now helloPromise has resolved

const event = await helloPromise;
console.log(event); // outputs: 'world'

Alternatively, a filter can be provided as part of the options object.

waitForEvent(emitter, 'hello', {
  filter: (value) => value === 'world'
});

A timeout can also be used to ensure you don't indefinitely wait on an event. If the emitter does not emit the proper event before the timeout, the promise will reject.

const emitter = new EventEmitter();

// wait for 1000 ms before rejecting
const helloPromise = waitForEvent(emitter, 'hello', {
  timeout: 1000
});

try {
  await helloPromise;
} catch (err) {
  // after 1000 ms has passed
  console.err(err);  // err.message === "Timed out waiting for event"
}

A timeout can also be used with a filter function.

waitForEvent(emitter, 'hello', (value) => {
  return event === 'hello';
}, { timeout: 1000 });

// alternatively, you can use supply the filter function in the options
waitForEvent(emitter, 'hello', {
  timeout: 1000,
  filter: (event) => event === 'world'
});

A logging function can be passed in to get more information about what the module is doing.

const helloPromise = waitForEvent(emitter, 'hello', {
  logger: console.debug.bind(console),
  filter: (event) => event === 'hello'
});

emitter.emit('hi');
emitter.emit('hey');
emitter.emit('yo');
emitter.emit('hello');

In your logs, you will see the following output:

Waiting for event: hello
Filtering for event: hello
Filtering for event: hello
Filtering for event: hello
Got matching event: hello