title: Refactoring nested callbacks, node.js, async
body: <pre><code>function indexArticles(callback) {
    fs.readdir("posts/", function(err, files) {
      async.map(files, readPost, function(err, markdown) {
        async.map(markdown, parse, function(err, results) {
          async.sortBy(results, function(obj, callback) {
            callback(err, obj.date);
          }, function(err, sorted) {
            callback( {"articles": sorted.reverse()} );
          });
        });
      });
    });
  }
  </code></pre>
  
  <p>I'm trying to figure out how to make this prettier -- as you can tell I'm using caolan's async library, but I'm not sure which of the control flow structures to use. It seems like if I use async.waterfall, for example, that results in quite a bit more code, with each step having to be wrapped in an anonymous function. For example, this is just the first two lines of the nested version with waterfall:</p>
  
  <pre><code>function indexArticles(callback) {
    async.waterfall([
      function(callback) {
        fs.readdir("posts/", function(err, files) {
          callback(err, files)
        })
      },
  
      function(files, callback) {
        async.map(files, readPost, function(err, markdown) {
          callback(err, markdown)
        })
      }])
  }
  </code></pre>
  
  <p>How would you improve this?</p>
  
  <p>If there were a way to partially apply arguments NOT only from the left, then I could see doing, for example,</p>
  
  <pre><code>function indexArticles(callback) {
    async.waterfall([
      async.apply(fs.readdir, "posts/"),
      async.apply(async.map, __, readPost),
      async.apply(async.map, __, parse),
      // etc...
    ])
  }
  </code></pre>