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

promise-event-emitter

v0.1.0

Published

EventEmitter like class which uses a promise instead of a callback

Downloads

16

Readme

promise-event-emitter

EventEmitter like class which uses a promise instead of a callback.

Installation

npm package

> npm install --save promise-event-emitter
const PromiseEventEmitter = require('promise-event-emitter');

Quickstart

const PromiseEventEmitter = require('promise-event-emitter');
const emitter = new PromiseEventEmitter();

const eventPromise = emitter.on('connect', 'error')
    .then(([userId, connection]) => {
        console.log(userId, connection) // 'Ivan', {}
    })
    .catch(([reason]) => {
        console.error(reason) // connection refused
    });


emitter.emit('connect', 'Ivan', {}); // in this case eventPromise become fullfilled
emitter.emit('error', 'connection refused'); // in this case eventPromise become rejected
emitter.off('connect'); // in this case by default eventPromise will never be fulfilled or rejected

API

This package requires ES6 Promise and Map.

emitter.on(successEvent: String || [String], rejectEvent: String || [String]) => Promise

Returns a promise that is waiting for emit first event of the arguments. successEvent and rejectEvent can be a string or a string array. Promise handler will get an array of arguments passed to emit function. This method is syntax sugar for 'once' method.

const eventPromise = emitter.on('connect', ['error', 'fail']); 
/* eventPromise become:
    fullfilled when emit 'connect' event 
    rejected when emit 'error' or 'fail'
*/

emitter.emit(event: String, ...args) => Boolean

Emits a event and fulfills promises of subscribers with array of passed arguments. Then it removes the specified event from the event-promise map. Returns true if the event had listeners, false otherwise.

emitter.once('message').then(console.log); // ['Ivan', 'hello world!']
emitter.emit('message', 'Ivan', 'hello world!');

emitter.off(event: String, type: String = 'pass', reason: Any = 'Handler removed') => Boolean

Removes the specified event from the event-promise map. Arguments 'type' and 'reason' is optional. Argument 'type' set behavior to promises of subscribers. It can be one of the following strings:

  • pass(by default) - do nothing
  • resolve
  • reject

Argument reason will be passed as argument to handler. Returns true if the event had listeners, false otherwise.

emitter.on('message').catch(console.error) // ['the connection is lost']
emitter.off('message', 'reject', 'the connection is lost');

emitter.once(event: String) => Promise

Find by event registered promise in event-promise map or create it. Returns a promise that will be fulfill when the event will be emitted.

const promise1 = emitter.once('connect');
const promise2 = emitter.once('connect');
console.log(promise1 === promise2) // true

promise1.then(console.log); // ['Jack']
promise2.then(console.log); // ['Jack']
emitter.emit('connect', 'Jack');

emitter.all(events: [String]) => Promise

Returns a promise that will be fulfill when all event will be emitted.

emitter.all(['connect', 'message'])
    .then(console.log); // [['Ivan'], ['Hello world!']]
emitter.emit('connect', 'Ivan');
emitter.emit('message', 'Hello world!');

emitter.eventNames() => [String]

Returns an array of event names that are registered in the event-promise map.

emitter.on('connect', ['error', 'fail']);
console.log(emitter.eventNames()) // ['connect', 'error', 'fail'];

emitter.eventPromiseMap

Each instance of the promise-event-emitter class stores an event-promise Map. This allows any type of data to be used as event.

const event = new Event('myEvent');
emitter.once(event).then(console.log); // [42];
emitter.emit(event, 42);

License

ISC © Letry