events-bluebird
v1.0.2
Published
events.EventEmitter wrapper implements `emitAsync` method which awaits promises returned from event listeners
Downloads
13
Maintainers
Readme
events-bluebird
This is a lightweight wrapper for native node events.
Adds EventEmitter.prototype.emitAsyncSeries
& EventEmitter.prototype.emitAsyncParallel
methods. The API reflects native EventEmitter
API with the following necessary changes:
- Added methods return a Promise object which when fulfilled, returns
true
if the event had listeners,false
otherwise - node's deprecated domain feature is not supported
Installation
npm install events-bluebird
Example
var EventEmitter = require('events-bluebird').EventEmitter;
var emitter = new EventEmitter;
emitter.on('event', function(param) {
return new Promise(function(resolve, reject) {
setTimeout(function() {
console.log('first');
return resolve();
}, 500);
});
});
emitter.on('event', function(param) {
console.log('later');
});
emitter.emitAsyncSeries('event', 'param').then(function(hadListeners) {
console.log(hadListeners);//true
});
// Output:
// first
// later
// true
Tests
npm test