title: Node.js Asynchronous Library Comparison - Q vs Async
body: <p>I have used <a href="https://github.com/kriskowal/q">kriskowal's Q library</a> for a project (web scraper / human-activity simulator) and have become acquainted with promises, returning them and resolving/rejecting them, and the library's basic asynchronous control flow methods and error-throwing/catching mechanisms have proven essential.</p>
  
  <p>I have encountered some issues though. My <code>promise.then</code> calls and my callbacks have the uncanny tendency to form pyramids. Sometimes it's for scoping reasons, other times it's to guarantee a certain order of events. (I suppose I might be able to fix some of these problems by refactoring, but going forward I want to avoid "callback hell" altogether.) </p>
  
  <p>Also, debugging is very frustrating. I spend a lot of time <code>console.log</code>-ing my way to the source of errors and bugs; after I finally find them I will start throwing errors there and catching them somewhere else with <code>promise.finally</code>, but the process of locating the errors in the first place is arduous.</p>
  
  <p>Also, in my project, <strong>order matters</strong>. I need to do pretty much everything sequentially. Oftentimes I find myself generating arrays of functions that return promises and then chaining them to each other using <code>Array.prototype.reduce</code>, which I don't think I should have to do.</p>
  
  <p>Here is an example of one of my methods that uses this reduction technique:</p>
  
  <pre><code>removeItem: function (itemId) {
  
    var removeRegexp = new RegExp('\\/stock\\.php\\?remove=' + itemId);
  
    return this.getPage('/stock.php')
    .then(function (webpage) {
      var
        pageCount = 5,
        promiseFunctions = [],
        promiseSequence;
  
      // Create an array of promise-yielding functions that can run sequentially.
      _.times(pageCount, function (i) {
        var promiseFunction = function () {
          var
            promise,
            path;
  
          if (i === 0) {
            promise = Q(webpage);
          } else {
            path = '/stock.php?p=' + i;
            promise = this.getPage(path);
          }
  
          return promise.then(function (webpage) {
            var
              removeMatch = webpage.match(removeRegexp),
              removePath;
  
            if (removeMatch !== null) {
              removePath = removeitemMatch[0];
  
              return this.getPage(removePath)
              .delay(1000)
              // Stop calling subsequent promises.
              .thenResolve(true);
            }
  
            // Don't stop calling subsequent promises.
            return false;
  
          }.bind(this));
        }.bind(this);
  
        promiseFunctions.push(promiseFunction);
      }, this);
  
      // Resolve the promises sequentially but stop early if the item is found.
      promiseSequence = promiseFunctions.reduce(function (soFar, promiseFunction, index) {
        return soFar.then(function (stop) {
          if (stop) {
            return true;
          } else {
            return Q.delay(1000).then(promiseFunction);
          }
        });
      }, Q());
  
      return promiseSequence;
    }.bind(this))
    .fail(function (onRejected) {
      console.log(onRejected);
    });
  },
  </code></pre>
  
  <p>I have other methods that do basically the same thing but which are suffering from much worse indentation woes.</p>
  
  <p>I'm considering refactoring my project using <a href="https://github.com/caolan/async">coalan's async library</a>. It seems similar to Q, but I want to know exactly how they differ. The impression I am getting is that async more "callback-centric" while Q is "promise-centric".</p>
  
  <p><strong>Question:</strong> Given my problems and project requirements, what would I gain and/or lose by using async over Q? How do the libraries compare? (Particularly in terms of executing series of tasks sequentially and debugging/error-handling?)</p>