5 Reasons To Use Bluebird for Promises
ES6 promises are great, but Bluebird remains compelling.
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:
- 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.
- 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.
- 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.
- 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. 😁
- You can promisify existing APIs via promisifyAll. Run promisifyAll on any API and convert it into a promise-based API.
- Bluebird weighs 17K gzipped.
- It extends the promise spec, so Bluebird’s extra features may confuse some at first.
- The extra features above are yet another thing to learn.
Worth the downsides? Other benefits I’m overlooking? Chime in via the comments.