The shortest syntax for defining async flow. It helps to handle difficult cases with many parallel async operations.
This lib is experimental. Its API may change soon
npm i -S brief-async
- At the start we have a value
'foo'and want to immediately start 3 functions parallel.
- Then, after both
fn2are finished, their results become an argument for
- Then, after both
fn4are finished, a
finishfunction will start using their results.
// fn1-fn4 and finish functions are declared somethere above.const flow = ;const steps ='foo' fn1 fn2 fn3fn1 fn2 fn4fn3 fn4 finish;const result = await ;
steps is just an array. It contains functions and their dependencies.
- The first element is an array of dependencies which will be resolved and then taken by the second element.
- The second element must be a function or it will be ignored instead.
- The third element may be a function or an array.
const steps =// 'foo' is the one argument for fn1-fn3 which work parallel'foo' 123 fn1 fn2 fn3 deadEnd// f1 and fn2 after resolving pack their results to an array which is the first argument for fn4fn1 fn2 fnArg fn4// results of f3 and fn4 become go to finish as an arrayfn3 fn4 finish;// result is a Promise// errHandler is a callback to execute when a reject function is called// if isMilestones == true, intermediate results will be included to the final resultconst result = ;
Step handlers or executors are functions with 3 arguments:
- Data from the previous step (array or a single value).
- Resolve callback.
- Reject callback.
2-nd and 3-rd arguments is needed to finish a Promise. An executor function must call one of those callbacks instead of using
return statement. The first argument is polymorphic: a value or an array; it depends on the count of dependencies.
deadEnd does not have
resolve function as an argument.
But it has a
So, "dead end" functions may break the whole chain, but they can not resolve the chain.