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.

Official recommendation for using Bluebird with async functions?

See original GitHub issue

Hello folks. First off I recognize that (despite your support guidelines) I am asking a support question, that has already received an answer on Stack Overflow. But a disadvantage of that channel is that I can’t tell if that’s an “official” recommendation or just some random user trying to be helpful.

I also feel like this discussion should be documented somewhere on bluebirdjs.com, whereas currently async functions don’t get any mention there. (Closest is a mention of C#'s async functionality on Promise.coroutine. But what about in JS? async/await have been natively supported for multiple versions of V8 now.)

So—do the Bluebird maintainers have any position on the best way to use Bluebird with async functions, with an eye to functionality and/or performance? Suggestions:

  • somehow cause async functions to return instances of Bluebird promises rather than native promises? (Noting that changing global.Promise is not sufficient as shown here and here. It sounds like this guy may have found a way to do this but he doesn’t show his code unfortunately. I have a comment pending on that post, we’ll see.)
  • wrap async functions in Bluebird.method?
  • use a Babel plugin like this to convert async methods into Bluebird’s coroutine and method helpers?

Related: https://github.com/petkaantonov/bluebird/issues/1426#issuecomment-318286431.

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:2
  • Comments:5

github_iconTop GitHub Comments

25reactions
benjamingrcommented, Aug 7, 2017

Basically, over the years we’ve added to the platform all the features that made native promises awful back then (swallowing errors) and from the other side there has been a lot of effort from V8 to optimize native promises and from platforms to provide stable APIs for them.

My recommendation would be to use bluebird for collection methods and when you need performance over async functions (that might change) while you should likely use native promises for everywhere else.

The big pain points bluebird aimed to solve were addressed by native promises. Bluebird still has very useful APIs (like promisification of a whole module, cancellation or collection methods).

  • For promisification use promisifyAll to promisify a whole module and util.promisify for smaller stuff. We only recently got util.promisify and it probably will get better
  • If you need to support older environments or are building a library that might - prefer bluebird.
  • For collection methods it is very useful.
  • There are still cases where bluebird’s performance is considerably better.

Other than that - prefer native promises. They are the future. There is no official way to use bluebird promises with async/await - (there was a proposal for that that was for observables but would have worked with bluebird - it got turned down). Using a babel plugin works - but if you care about performance Babel isn’t great anyway.

Bluebird has 17.5 million downloads a month - I hope the native platform gets good enough to get those numbers down - and we’re working on it 😃

6reactions
wearherecommented, Aug 8, 2017

Bluebird.coroutine is still likely faster - but note that TurboFan can do optimizations that bluebird cannot with async functions - and over time it will get better.

Super cool, thanks for the information @benjamingr, and for your / the other maintainers’ hard work over the years to get the platform to this point!

That’s cool on the docs, just wanted to offer in return for you posting everything here. 😃 Closing for now then.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Getting Started | bluebird
Bluebird is a fully featured JavaScript promises library with unmatched performance.
Read more >
Bluebird v2.9.27 API Reference
Without arrow functions that provide lexical this , the correspondence between async and sync code breaks down when writing object-oriented code. .bind() ...
Read more >
Bluebird's Bad-Practice Docs. The Terrible Best ... - ITNEXT
While you probably won't want to use both the native implementation and Bluebird at the same time, my recommendation is to always import ......
Read more >
Using Promises in Node.js Applications using Bluebird
If there is one more dependent async operation, we can perform that in callback of then function on the last promise. This process...
Read more >
4. Using Libraries and Frameworks - JavaScript with Promises ...
Bluebird provides a convenient function named promisify that wraps node-style functions with ones that return a promise, as shown in Example 4-7. Example...
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