Add `Bluebird.on()` for promisifying an event emitter
See original GitHub issueIt’s incredibly common to need to promisify event emitters of various types, particularly streams. I’ve written something like this a million times (approximately):
function promisifyEmitter (emitter, success = "data", error = "error") {
return new Promise((resolve, reject) => {
emitter.on(success, resolve).on(error, reject);
});
}
It would be really nice to have this as part of bluebird, perhaps in the form of Bluebird.on()
:
Bluebird.on = function (emitter, success, error) {
if (success === undefined) success = "data";
if (error === undefined) error = "error";
return new Promise(function (resolve, reject) {
if (Array.isArray(success)) {
for (var i = 0; i < success.length; i++) {
emitter.once(success[i], resolve);
}
}
else {
emitter.once(success, resolve);
}
if (Array.isArray(error)) {
for (var i = 0; i < error.length; i++) {
emitter.once(error[i], reject);
}
}
else {
emitter.once(error, reject);
}
});
};
Issue Analytics
- State:
- Created 8 years ago
- Reactions:1
- Comments:14 (1 by maintainers)
Top Results From Across the Web
Bluebird Promises with Event Emitter - node.js - Stack Overflow
I am fairly new with using Bluebird promises. I was trying to use them over an emitter. However, I am stuck on how...
Read more >Promisify Javascript Events with Bluebird | by Ted Kulp - Medium
var events = require('events'); exports = module.exports = new events.EventEmitter();. I found myself using a common pattern of wanting to have ...
Read more >Promise.promisifyAll - Bluebird JS
Promisifies the entire object by going through the object's properties and ... filter with promisifier for the restler module to promisify event emitter:....
Read more >bluebird-events: Documentation | Openbase
This package wraps an event emitter object and returns a bluebird promise that is either resolved, or rejected based on what events are...
Read more >bluebird-events - NPM Package Overview - Socket - Socket.dev
A wrapper around event emitters that returns a bluerbird promise resolved or ... { promisify } from 'bluebird-events' const someEmitter = new TestEmitter()...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Sorry to dig out this old ticket, but I did not understand @petkaantonov’s last comment.
It is the developer choice to properly choose what event he wants to await on. Especially nowadays, since we have async/await available. For example, being able to await on the “end” event of a stream is quite useful. If one needs to wait for another event, she can just use Promise.all.
@benjamingr I just created https://github.com/nodejs/node/issues/20893