ctrlflow#
Asyncronous control flow is important in most node.js programs,
and it seems that every one has written a library to prevent callbacks getting out of hand.
most notably, creationix's step
SubStack's seq
, isaacs's slide
, and caolan`s async.
These modules all do somethings right, but still leave room for improvement.
ctrlflow
combines the best features of these modules with a simple & flexible API and a
focus on robust error handling.
var go = ctrl(ArrayOFSteps); go(args,...,callback).
ctrl
takes an array of steps, and combines them into one asyncronous function that will call
each step in sequence, and callback when the last step finishes, or when a step errors.
each step is wrapped in a try ... catch, and if a function throws, it will stop executing the steps and pass the error to the final callback.
basic example
var go =
This example illistrates several things.
Firstly, seq
returns an ayncronous function, go
.
The args passed to go
are passed to the first step, fs.readFile
,
and the results of fs.readFile
(minus the err parameter) are passed to the second step,
which parses the file, then callsback.
If the file does not exist, readFile will callback with an error.
If the file exists, but is not valid JSON, JSON.parse
will throw syncronously.
(this will be caught be seq
, so beware that mulitple types of errors be passed to the callback.
parallel group example
Sometimes you want to several async steps in parallel, ctrlflow has a literal syntax for this too!
a simple usecase for this is to call stat on a file, and, just incase it is a symbolic link, call lstat as well. (lstat will stat the link file, not the file it links to)
filename {console}
all together
var ctrl =var go =ctrl//seq returned a function, call it, passing a callback:
ERROR HANDLING
if any step throws or callsback with an error,
the callback passed to go
will be called with the error.
no further steps will be called.
var ctrl =var go =ctrl//seq returned a function, call it, passing a callback:
grabbing the callback
the callback is always added as the last argument, and as this.next
the best way to get the callback is to pop it off the arguments
var callback = slice