2.0.1 • Published 7 years ago

pattern-emitter-promise v2.0.1

Weekly downloads
2
License
MIT
Repository
github
Last release
7 years ago

pattern-emitter-promise

Build Status npm version

Asynchronous (using ES2015 Promises) event emitter supporting regex listeners

Installation

Just do:

npm install pattern-emitter-promise

and you are ready.

This lib has no external dependencies. Library should be used in environment with Promise global constructor. If there is no such constructor (node 0.10 and earlier or some older browsers), Promise library must be set before first instantiation using setPromiseLibrary static method.

Usage

In your code:

import { EventEmitter } from 'event-emitter-promise';

let ee = EventEmitter();

ee.on('fooBar', () => {
    return Promise.resolve('fooBar1');
});

ee.on(/^foo/, (bar) => {
    return Promise.resolve('foo' + bar + '2');
});

ee.on('fooBar', () => {
    return 'fooBar3'; //we support sync return too
});

ee.emit('fooBar', 'Bar').then((results) => {
    results; // ['fooBar1', 'fooBar2', 'fooBar3']
});

For more usage examples see tests and read docs in the code.

License

MIT