Super-simple control flow
You want to bake a cake. You could do everything sequentially:
valve(preheatOven, mixBatter, bakeMix, serveCake)
But you're a programmer. You want to be efficient. So you mix the batter while the oven is preheating:
valve([preheatOven, mixBatter], bakeMix, serveCake)
app.get('/timeline', loadAds, loadUser, logRequest, ...)
logRequest in sequence. But let's
say that while
logRequest requires the ad and user to be loaded,
loadUser could fire simultaneously. Then the efficient version would be
app.get('/timeline', valve([loadAds, loadUser], logRequest), ...)
No further changes required.
valve takes a series of functions, which are run in sequence. Arrays within
the series are run in parallel.
The function returned by
valve can be run multiple times. So you might write
flow = valve([readFile1, readFile2], writeConcatenation)
and then call
flow(callback) every time you want to carry out that set of
When you call the function returned by
n arguments, where the
last is a callback, the first
n-1 arguments are passed to each function,
along with Valve's own callback.
Valve assumes that each function's callback takes the form
(err, results...). If any function provides an
err other than
undefined, Valve invokes no further functions and passes
err to the main
callback. The main callback receives the results as an array of arrays; so the
first result from the fifth function given to Valve would be
Valve does not make the results of previous functions available to later functions. You're on your own for sharing state within the flow.