Last Updated: February 25, 2016
·
1.086K
· rjz

Async, partials, node, and you.

The async library provides invaluable utilities for wrangling multi-step asynchronous tasks:

function copyFile (src, dst, done) {
  async.waterfall([
    function (callback) {
      fs.readFile(src, callback);
    },
    function (result, callback) {
      fs.writeFile(dst, result, callback);
    }
  ], done);
}

But know what's even better than using async to help wrangle nested callbacks in check? Using async and _.partial to avoid those wrapping functions entirely:

function copyFile (src, dst, done) {
  async.waterfall([
    _.partial(fs.readFile, src),
    _.partial(fs.writeFile, dst)
  ], done);
}

(Example borrowed from Trimming the Callback Tree)