DailyJS

Bluebird

Alex R. Young

Subscribe

@dailyjs

Facebook

Google+

libraries node async promises

Bluebird

Posted by Alex R. Young on .
Featured

libraries node async promises

Bluebird

Posted by Alex R. Young on .

The never-ending quest for the perfect async/promises library continues. Petka Antonov's offering, Bluebird (GitHub: petkaantonov / bluebird, License: MIT, npm: bluebird), has all of your favourite APIs, impressive benchmarks, and Mocha unit tests. It works in browsers as well.

The Bluebird API reference has explanations of each method, and code examples. Here's an example of using promises and finally to make an XMLHttpRequest:

function ajaxGetAsync(url) {  
  return new Promise(function(resolve, reject) {
    var xhr = new XMLHttpRequest;
    xhr.addEventListener('error', reject);
    xhr.addEventListener('load', resolve);
    xhr.open('GET', url);
    xhr.send(null);
  }).finally(function() {
    $('#ajax-loader-animation').hide();
  });
}

Petka points out that the finally method works like Q's finally method. The catch/caught method also works like Q (with fail renamed to caught), which is a bit of sugar for .then(null, errorHandler).

One of the tools Petka mentions for verifying the quality of the project was async-compare by Gorgi Kosev. This project aims to compare async patterns based on their complexity, performance, and "debuggability". The stack traces generated by libraries like Bluebird and Q are captured and curated, so async-compare can be used to measure the distance between the function that created the error and the error in the stack trace. This is useful because debugging asynchronous code is notoriously difficult.

Even though Q and popular Node modules like async are good, I think Bluebird is worth looking at because it seems to have attention to the right details.