Last Updated: February 25, 2016
·
4.668K
· tralamazza

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.