Determine the best order for running async functions, LIKE MAGIC!
npm install run-auto
Determines the best order for running the functions in
tasks, based on their
requirements. Each function can optionally depend on other functions being completed
first, and each function is run as soon as its requirements are satisfied.
If any of the functions pass an error to their callback, the
auto sequence will
stop. Further tasks will not execute (so any other functions depending on it will
not run), and the main
callback is immediately called with the error.
Functions also receive an object containing the results of functions which have completed so far as the first argument, if they have dependencies. If a task function has no dependencies, it will only be passed a callback.
tasks- An object. Each of its properties is either a function or an array of requirements, with the function itself the last item in the array. The object's key of a property serves as the name of the task defined by that property, i.e. can be used when specifying requirements for other tasks. The function receives one or two arguments:
resultsobject, containing the results of the previously executed functions, only passed if the task has any dependencies, Argument order changed in 2.0
callback(err, result)function, which must be called when finished, passing an
error(which can be
null) and the result of the function's execution. Argument order changed in 2.0
callback(err, results)- An optional callback which is called when all the tasks have been completed. It receives the
errargument if any
taskspass an error to their callback. Results are always returned; however, if an error occurs, no further
taskswill be performed, and the results object will only contain partial results.
var auto =
Note, all functions are called with a
results object as a second argument, so it is
unsafe to pass functions in the
tasks object which cannot handle the extra argument.
For example, this snippet of code:
will have the effect of calling
readFile with the results object as the last argument,
which will fail, like this:
Instead, wrap the call to
readFile in a function which does not forward the
This module is basically equavalent to
async.auto, but it's
handy to just have the one function you need instead of the kitchen sink. Modularity!
Works great in the browser with browserify!
MIT. Copyright (c) Feross Aboukhadijeh.
Image credit: Wizard Hat designed by Andrew Fortnum