Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Super-simple control flow
CoffeeScript
branch: master

Fetching latest commit…

Cannot retrieve the latest commit at this time

Failed to load latest commit information.
lib
node_modules
src
Cakefile
README.mdown
package.json
test.coffee

README.mdown

Valve

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)

Done!

Real-world use

Valve is ideal for chaining route middlewares in Express. You might currently write

app.get('/timeline', loadAds, loadUser, logRequest, ...)

which executes loadAds, loadUser, and logRequest in sequence. But let's say that while logRequest requires the ad and user to be loaded, loadAds and loadUser could fire simultaneously. Then the efficient version would be

app.get('/timeline', valve([loadAds, loadUser], logRequest), ...)

No further changes required.

Details

Creating flows

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 operations.

Function forms

When you call the function returned by valve with 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 null or 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 results[4][0].

Valve does not make the results of previous functions available to later functions. You're on your own for sharing state within the flow.

License

http://trevorburnham.mit-license.org

Something went wrong with that request. Please try again.