Skip to content

alxarch/events-as-promised

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Events as promised

An EventEmitter that's full of Promise!

Augment EventEmitter to handle asynchronous listeners by using a promise-returning emit() method. All listeners are executed serially, casting the return value to a Promise via Promise.resolve(). If a promise is rejected, or an error is thrown when executing the listener, the iteration stops and the resulting promise is rejected.

Usage

var Events = require("events-as-promised");
var events = new Events();
events.on("foo", function (bar, baz) {
	console.log("First listener");
});
events.on("foo", function (bar, baz) {
	console.log("Second listener");
	return Promise.resolve();
});
events.on("foo", function (bar, baz) {
	console.log("Third listener");
	return Promise.reject(new Error("Foo error"));
});
events.on("foo", function (bar, baz) {
	console.log("Fourth listener");
});

events.emit("foo").catch(function (err) {
	console.error(err.message);
});

// Will print
// First listener
// Second listener
// Third listener
// Foo error

No native Promise

Falls back to bluebird if no native Promise is found.

About

An `EventEmitter` that's full of `Promise`!

Resources

License

Stars

Watchers

Forks

Packages

No packages published