raco
Generator based flow-control that supports both callback and promise.
npm install raco
Many existing flow-control libraries such as co, assume promises to be the lowest denominator of async handling. Callback function requires promisify patch to be compatible, which creates unnecessary complication.
In raco, both callbacks and promises are yieldable. Resulting function can be called by both callbacks and promises. This enables a powerful control flow while maintaining simplicity.
raco(fn*, [opts])
Resolves a generator function. This does not return a Promise; uncaught error will be thrown.
// import racovar raco = ...
Yieldable callback works by supplying an additional next
argument.
Yielding non-yieldable value pauses the current generator,
until next(err, val)
being invoked by callback.
val
passes back to yielded value, or throw
if err
exists.
fn = raco.wrap(fn*, [opts])
Wraps a generator function into regular function that optionally accepts callback or returns a promise.
var fn = raco // Use with callback // use with promise
raco.wrapAll(obj, [opts])
Wraps generator methods of class or object.
* { ... } * { ... } // wrap prototype objectraco var app = appapp
Options
Calling raco with options object makes a factory function with a set of available options:
var raco = Promise: null // disable Promise { // custom yieldable } prepend: true // prepend or append `next` argument
opts.Promise
Raco uses native promise by default. This can be overridden by setting raco.Promise
.
var raco = Promise:
opts.prepend
By default, next(err, val)
function appends to arguments fn* (args..., next)
.
If opts.prepend
set to true
, generator function is called with fn* (next, args...)
.
This can be useful for functions that accept varying numbers of arguments.
var raco = var fn = raco
opts.yieldable
By default, the following objects are considered yieldable:
- Promise
- Generator
- Generator Function
- Thunk
It is also possible to override the default yieldable mapper. Use with caution:
- Takes the yielded value, returns
true
to acknowledge yieldable. - Callback
cb(err, val)
to resolve the yieldable.
var raco = { // map array to Promise.all if Array Promiseallval return true // acknowledge yieldable // Anything can be mapped! if val === 689 return true // acknowledge yieldable return false // acknowledge non-yieldable }
License
MIT