Asynchronous callback chains with support for caolan's async module
Create asynchronous callback chains. Make them powerful with built-in support for caolan's excellent async module.
Create a few functions that adhere to the error-first callback convention in node. Chain them together to create a new function.
var chain = require'async-chains'return stoUpperCasecbnull arrmapcapcbnull arrjoin' 'var arrayToCapString = chaincapEach toStringarrayToCapStringprocessargvslice2console.logresult
When you invoke the new function, the arguments you invoke it with are passed to the first function in the chain. The callback arguments of that function are passed to the next function in the chain, and so on. If any function passes an error to a callback, the remainder of the steps in the chain are skipped, and the final callback receives the error.
async-chains was written to make expressing task sequences simpler, especially when leveraging caolan's async module, while maintaining the node callback pattern. Here's an example of a chain that leverages async to do some file ops:
var chain = require'async-chains'async = require'async'fs = require'fs'path = require'path'fsreaddirdircberr filesmapreturn pathjoindir freturncbstrmatchpattern ? true : falsefsstatfilecberr file: file stats: statsvar getVimfileStats = chainreaddirFullpathchainlinknoErrorasyncfilter reFilter/\.vim[^\/]*$/chainlinkasyncconcat fileStatsgetVimfileStatsprocessenvHOMEif err return console.errorerrmessageresultforEachconsole.log'%s, last modified on %s' pathbasenameitemfile itemstatsmtime
In this example, we use chain.link to interface with async functions. The async collection function argument pattern, for the most part, is: array, iterator, callback. The array and callback args will be supplied from the previous step in the chain, but in order to inject the iterator we want to use, we use chain.link(asyncCollectionFunction, iteratorFunction).
var chain = require'async-chains'
Create a new
function (arg1, arg2, ..., finalCallback) that when invoked will have it's args passed to fn1 allong with a callback. When fn1
calls the supplied callback, the result of that callback is supplied as an argument to fn2, etc. If any callback is supplied an error argument,
no further chain steps will be called, and the final callback will be immediately invoked with the error. Once the final function in the chain
completes and calls it's callback, the arguments are passed to the finalCallback.
Returns a function usable in a chain that calls the async collection function (collFn) given with any additional parameters needed (such is the case with the limit variety such as eachLimit), and iterator. The result of the previous chain step's output will be supplied as the first argument to the async collection function.
This returns a function usable in a chain, as above, but should be used on async functions that do NOT return an error object. One such example is async.filter.
Create a function suitable for use as a standard node callback that feeds into a chain. Example:
var lastChange = chainchainlinkasyncconcat fsstat chainlinkasyncreduce 0cbnull itemmtime > memo ? itemmtime : memofsreaddirprocesscwd chaintolastChangeconsole.logresult
When a chain function is called, an event emitter is returned. This emitter emits
complete events. None of these events are gauranteed.
For example, a complete event is never emitted if an error is passed to the finalCallback. Nonetheless, these events may be useful for tracking
var chainFn = chainfn1 fn2 fn3var progress = chainFnif err console.errorerrprogresson'step'console.log'chainFn step %s executed' stepon'complete'console.log'chainFn complete'
A step event is emitted for each function in the chain that is executed. Two arguments are passed to the listener, the first being the step index (0-based), and the second argument is an array of arguments that was passed to the function at that step.
A complete event is emitted once the chain function called the final callback. The listener of this event received a single argument, which is an array of arguments that was passed to the final callback.