5 Reasons To Use Bluebird for Promises

ES6 promises are great, but Bluebird remains compelling.

Bluebird.js — A JavaScript Promises library

It’s great that the web platform keeps adding native support for popular approaches. I was excited about the release of ES6 promises. However, here are 5 reasons you may want to use Bluebird instead of native ES6 promises:

  1. Error pattern matching in catch. This means you can declare and trap specific error types, much like you can today in statically typed languages like C# and Java. This also has the benefit of being faster than a plain catch.
  2. Bluebird promises can be cancelled! 👍 It’s a shame this feature isn’t in the promise spec. Cancelling an AJAX call is often necessary to avoid throwing errors when the user navigates away while the call is in progress.
  3. Bluebird can create a synchronous API from asynchronous code by wrapping generators in promise.coroutine. This means calling code need not worry about the promise or generator that’s running behind the scenes in a given function. It can call the function like it’s a simple synchronous function. This pattern can really clean up calling code.
  4. Ever forgotten to declare a .catch? If you do, traditional ES6 promises will swallow the error. 😡 With Bluebird, unhandled errors are not silently swallowed by default, but reported along with helpful stack traces. 😁
  5. You can promisify existing APIs via promisifyAll. Run promisifyAll on any API and convert it into a promise-based API.

Downsides

  1. Bluebird weighs 17K gzipped.
  2. It extends the promise spec, so Bluebird’s extra features may confuse some at first.
  3. The extra features above are yet another thing to learn.

Worth the downsides? Other benefits I’m overlooking? Chime in via the comments.


Cory House is the author of multiple courses on JavaScript, React, clean code, .NET, and more on Pluralsight. He is principal consultant at reactjsconsulting.com, a Software Architect at VinSolutions, a Microsoft MVP, and trains software developers internationally on software practices like front-end development and clean coding. Cory tweets about JavaScript and front-end development on Twitter as @housecor.