Last Updated: February 25, 2016
·
1.342K
· 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)

Have a fresh tip? Share with Coderwall community!

Post
Post a tip
Did you know?

JavaScript on a page runs with the privileges of that origin. One altered library, tag manager, or snippet can read inputs, change what people see, and call your APIs as the user.

Security insights from Cside