co-flow
Flexible execution flow addons (all, any, wait) for co.
Quickstart
const all = all;const any = any;const wait = wait; const allResults = ; // returns an array with the results of all runnablesconst anyResult = ; // returns the result for the runnable which succeeded first ; // just what you expect
Usage
all (runnables, [options])
Executes all runnables and waits for all of them to complete.
Returns an array with the result of each runnable, in the same order.
If options.failsWhenAnyFailed
is false
then the returned array will also contain errors for each runnable which failed.
Returns an empty array if runnables
evaluates to false
or is an empty array.
Parameters
-
runnables
- An array of runnables to be executed. -
options.concurrency
- Specifies the maximum number of runnables which will be executed concurrently (similar to tasks in a thread pool).
true
executes all runnables in parallel andfalse
is like1
and runs all runnables serially in order.
Defaults totrue
. -
options.failsWhenAnyFailed
- Iftrue
then the function will fail if any runnable failed.
The error of the first runnable which failed will be passed along.
Defaults totrue
. -
options.failsWhenAllFailed
- Iftrue
then the function will fail if all runnables failed.
The error of the first runnable which failed will be passed along.
Defaults tofalse
. -
options.structured
- Iftrue
then the function's result data will be wrapped with{ error: … }
or{ data: … }
, respectively.
Defaults tofalse
. -
options.this
- Value forthis
when executing runnables oroptions.unusedResultHandler
. -
options.unusedResultHandler
- Callback for each runnable whose result was not returned by the function (due tooptions.failsWhenAnyFailed
oroptions.failsWhenAllFailed
).
The callback will be called after the function returned and in the order of the runnables.
Example
Load multiple URLs concurrently.
const all = all;const co = ;const request = ; ;
any (runnables, [options])
Executes all runnables and only waits for one of them to complete successfully.
Returns the result data of the first runnable which completes successfully or else the error of the first runnable which failed if options.failsWhen(Any/All)Failed
are both false
.
Returns the result data undefined
if runnables
evaluates to false
or is an empty array.
Parameters
-
runnables
- An array of runnables to be executed. -
options.concurrency
- Specifies the maximum number of runnables which will be executed concurrently (similar to tasks in a thread pool).
true
executes all runnables in parallel andfalse
is like1
and runs all runnables serially in order.
Defaults totrue
. -
options.failsWhenAnyFailed
- Iftrue
then the function will fail as soon as any runnable failed.
As a consequence a successful completion of the function will be delayed until all runnables have completed.
The error of the first runnable which failed will be passed along.
Defaults tofalse
. -
options.failsWhenAllFailed
- Iftrue
then the function will fail if all runnables failed (instead of returning the error as result data).
The error of the first runnable which failed will be passed along.
Defaults totrue
. -
options.structured
- Iftrue
then the function's result data will be wrapped with{ error: … }
or{ data: … }
, respectively.
Defaults tofalse
. -
options.this
- Value forthis
when executing runnables oroptions.unusedResultHandler
. -
options.unusedResultHandler
- Callback for each runnable whose result was not returned by the function.
The callback will be called after the function returned and all runnables have completed. It will be called in the order of the runnables.
Example
Request multiple URLs concurrently and only wait for one request complete successfully.
const any = any;const co = ;const request = ; ;
wait (delay)
Pauses execution for the given delay - like setTimeout
in generator style.
Parameters
delay
- Time in milliseconds before the function returns and execution continues.
Example
const co = ;const wait = wait; ;
Installation
$ npm install co-flow
Requirements
Node 0.11+, run with --harmony
flag.
Testing
$ npm install
$ npm test
License
MIT