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)
Written by RJ Zaworski
Related protips
Have a fresh tip? Share with Coderwall community!
Post
Post a tip
Best
#Node
Authors
Sponsored by #native_company# — Learn More
#native_title#
#native_desc#