question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Add `Bluebird.on()` for promisifying an event emitter

See original GitHub issue

It’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:closed
  • Created 8 years ago
  • Reactions:1
  • Comments:14 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
kirly-afcommented, May 22, 2018

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.

try {
    await Promise.all([
        Promise.on('end', readbleStream),
        Promise.on('finish', writableStream),
        Promise.on('someEvent', writableStream)
    ]);
} catch (err) {
    // convenient error handling
}
0reactions
kirly-afcommented, May 22, 2018
Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found