Asynchronous Flow Programming Library for Node.js
This is a Node.js module which provides a DSL for simplifying asynchronous flow programming in Node.js.
Let example explain:
var Class = flow = ; var MyItem = ; var MyClass = ; ;
Installation
npm install js-flow
var flow = ;
Supported Flows
Array/Object Enumeration
flow
Or
Then the following options can be chained (the order is not important):
flowflow
Both Array
and Object
are supported.
If it is an Array
, index and value in each pair is enumerated.
If it is an Object
, key and value in each pair is enumerated.
flowflow
Specify the iterator.
For Function
, the arguments is like:
If the final run
method is passed a number of arguments, they will be forward to the iterator. "index/key" is present if withIndex
is used.
If String
is provided instead of Function
, it represents the method name in context provided by with
. In above example, names are used.
If String
is prefixed with &, the method of iterated object is invoked.
Map/Reduce can also be used:
flowflowflow
When map
is used, the callback passed to run
accepts the 2nd parameter with the results ofmap
. If reduce
is used, the arguments passed to run
is used as initial value for reduce
.
Also compatible methods as in javascript Array
:
flowflow
The maximum number of concurrent enumeration can be limited using:
flowflow // this is equivalent to concurrent(1)flow // allow unlimited concurrentcy
Sequential/Parallel Execution
flow nextFunction or String // do is alias of next next... flow
For sequential execution, when chain
is used, the result from previous step can be passed as arguments to next step:
flow next { ; } next { ; } // here result is 'abcd1'
For parallel execution, concurrency can be controlled the same way as Array/Object enumeration.
Times
This is simple and straightforward:
flow
Same concurrency control as above.
Loop
flow // infinite loopflow
In any iteration function (Loop, Enumeration, Times etc), passing string break
as the first argument of next
function can terminate the loop/enumeration.
For all function passed in, bind
is not necessary as .with(context)
will ensure the functions are invoked with correct context.
Extra callbacks
flow...flow...
successFunc
and errorFunc
are invoked synchronously before invoking callback
.
Arguments passed to successFunc
doesn't including Error
which is usually the first arguments for callbacks,
and errorFunc
is only passed single argument which is an instance of Error
.
Examples
Create directories in parallel
flow // .each([...]) is short for .each().in([...]) ;
Create directories in series
flow // equivalent to .concurrent(1) // use method name here, .with must be used ;
Send requests in parallel
var Request = ; flow // allow max 4 requests in parallel ;
Reduce responses
flow // allow max 4 requests in parallel ;
Arguments passed to run
flow next { ... } next { } ; // for all steps, factor1 = 12, factor2 = 15
Chained steps
flow next { ... ; } next { ... ; } ; // for all steps, factor1 = 12, factor2 = 15
Times
flow
While loop
flow ;
Do-while loop
flow ;
License
MIT/X11 License