Playing with async.auto()
Async .auto() can save you from writing a forest of .series()
and .parallel()
. If you want to reuse existing series/parallels callbacks, or just look silly, you can try the following hack:
function aapply(deps, fn) {
deps = [].concat(deps);
return deps.concat(function (next, hash) {
var args = deps.map(function (i) { return hash[i]; });
fn.apply(null, args.concat(next));
});
}
An example should be enough :)
async.auto({
foo: function (next) { next(null, 'foo'); },
bar: function (next) { next(null, 'man'); },
wat: aapply(['foo', 'bar'], function (a, b, next) {
next(null, a + b + 'xu');
})
}, console.log);
edited for reduced overall readability and making sure deps
is an array.
Written by Daniel Tralamazza
Related protips
Have a fresh tip? Share with Coderwall community!
Post
Post a tip
Best
#Async
Authors
Sponsored by #native_company# — Learn More
#native_title#
#native_desc#