feross / run-auto

Determine the best order for running async functions, LIKE MAGIC!
MIT License
87 stars 11 forks source link
async auto browser javascript nodejs

run-auto travis npm downloads javascript style guide

Determine the best order for running async functions, LIKE MAGIC!

auto Sauce Test Status

install

npm install run-auto

usage

auto(tasks, [callback])

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.

arguments
example
var auto = require('run-auto')

auto({
  getData: function (callback) {
    console.log('in getData')
    // async code to get some data
    callback(null, 'data', 'converted to array')
  },
  makeFolder: function (callback) {
    console.log('in makeFolder')
    // async code to create a directory to store a file in
    // this is run at the same time as getting the data
    callback(null, 'folder')
  },
  writeFile: ['getData', 'makeFolder', function (results, callback) {
    console.log('in writeFile', JSON.stringify(results))
    // once there is some data and the directory exists,
    // write the data to a file in the directory
    callback(null, 'filename')
  }],
  emailLink: ['writeFile', function (results, callback) {
    console.log('in emailLink', JSON.stringify(results))
    // once the file is written let's email a link to it...
    // results.writeFile contains the filename returned by writeFile.
    callback(null, { file: results.writeFile, email: 'user@example.com' })
  }]
}, function(err, results) {
  console.log('err = ', err)
  console.log('results = ', results)
})

usage note

Note, all functions are called with a results object as a second argument, so it is unsafe to pass functions in thetasks object which cannot handle the extra argument.

For example, this snippet of code:

auto({
  readData: async.apply(fs.readFile, 'data.txt', 'utf-8')
}, callback)

will have the effect of calling readFile with the results object as the last argument, which will fail, like this:

fs.readFile('data.txt', 'utf-8', cb, {})

Instead, wrap the call to readFile in a function which does not forward the results object:

auto({
  readData: function (cb, results) {
    fs.readFile('data.txt', 'utf-8', cb)
  }
}, callback)

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! Especially handy if you're serving to the browser and need to reduce your javascript bundle size.

Works great in the browser with browserify!

see also

license

MIT. Copyright (c) Feross Aboukhadijeh.

Image credit: Wizard Hat designed by Andrew Fortnum