Comparing version 2.0.0 to 2.0.1
48
index.js
@@ -15,2 +15,9 @@ var isGeneratorFunction = require('is-generator-function') | ||
/** | ||
* private caco resolver | ||
* | ||
* @param {function*} genFn - generator function | ||
* @param {array} args - arguments in real array form | ||
* @returns {promise} if no callback provided | ||
*/ | ||
function _caco (genFn, args) { | ||
@@ -21,9 +28,10 @@ var self = this | ||
// pass caco next to generator function | ||
if (typeof args[args.length - 1] === 'function') callback = args.pop() | ||
args.push(next) | ||
// callback stepper | ||
args.push(next) | ||
var iter = isGenerator(genFn) ? genFn : genFn.apply(self, args) | ||
function step (err, res) { | ||
// callback stepper | ||
function step (err, val) { | ||
if (!iter) { | ||
@@ -37,3 +45,3 @@ if (!done) { | ||
try { | ||
var state = err ? iter.throw(err) : iter.next(res) | ||
var state = err ? iter.throw(err) : iter.next(val) | ||
if (state.done) iter = null | ||
@@ -53,2 +61,3 @@ | ||
// callback stepper with nextTick delay | ||
function next () { | ||
@@ -75,2 +84,9 @@ var args = Array.prototype.slice.call(arguments) | ||
/** | ||
* caco resolver | ||
* | ||
* @param {function*} genFn - generator function | ||
* @param {...*} args - optional arguments | ||
* @returns {promise} if no callback provided | ||
*/ | ||
function caco (genFn) { | ||
@@ -81,2 +97,9 @@ var args = Array.prototype.slice.call(arguments, 1) | ||
/** | ||
* yieldable callback mapper | ||
* | ||
* @param {*} val - yielded value to resolve | ||
* @param {function} cb - resolver callback function | ||
* @returns {boolean} acknowledge yieldable | ||
*/ | ||
caco._yieldable = function (val, cb) { | ||
@@ -94,4 +117,4 @@ if (isPromise(val)) { | ||
} else if (isObservable(val)) { | ||
var dispose = val.subscribe(function (res) { | ||
cb(null, res) | ||
var dispose = val.subscribe(function (val) { | ||
cb(null, val) | ||
dispose.dispose() | ||
@@ -108,2 +131,9 @@ }, function (err) { | ||
/** | ||
* wraps a generator function into regular function that | ||
* optionally accepts callback or returns a promise. | ||
* | ||
* @param {function*} genFn - generator function | ||
* @returns {function} regular function | ||
*/ | ||
caco.wrap = function (genFn) { | ||
@@ -116,2 +146,8 @@ return function () { | ||
/** | ||
* wraps generator function properties of object | ||
* | ||
* @param {object} obj - object to caco.wrap | ||
* @returns {object} original object | ||
*/ | ||
caco.wrapAll = function (obj) { | ||
@@ -118,0 +154,0 @@ for (var key in obj) { |
{ | ||
"name": "caco", | ||
"version": "2.0.0", | ||
"version": "2.0.1", | ||
"description": "Generator based control flow that supports both callbacks and promises", | ||
@@ -5,0 +5,0 @@ "scripts": { |
@@ -24,8 +24,2 @@ # caco | ||
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, | ||
where `val` passes back to yielded value, or `throw` if `err` exists. | ||
Beware to handle uncaught errors. | ||
```js | ||
@@ -50,3 +44,3 @@ var caco = require('caco') | ||
}).catch(function (err) { | ||
// handle uncaught errors | ||
// handle uncaught error | ||
}) | ||
@@ -56,2 +50,6 @@ | ||
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, | ||
where `val` passes back to yielded value, or `throw` if `err` exists. | ||
#### `var fn = caco.wrap(fn*)` | ||
@@ -61,3 +59,3 @@ | ||
``` | ||
```js | ||
var fn = caco.wrap(function * (arg1, arg2, next) { | ||
@@ -83,4 +81,4 @@ yield setTimeout(next, 1000) // yield callback using 'next' | ||
App.prototype.fn = function * (next) { ... } | ||
App.prototype.fn2 = function * (next) { ... } | ||
App.prototype.fn = function * (next) {...} | ||
App.prototype.fn2 = function * (next) {...} | ||
@@ -92,7 +90,7 @@ // wrap prototype object | ||
app.fn(function (err, val) { ... }) | ||
app.fn(function (err, val) {...}) | ||
app.fn2().then(...).catch(...) | ||
``` | ||
## Yieldables | ||
## Yieldable | ||
@@ -141,4 +139,4 @@ By default, the following objects are considered yieldable: | ||
} | ||
}, mapper)(function (err, res) { | ||
// handle error or return | ||
}).catch(function (err) { | ||
// handle uncaught error | ||
}) | ||
@@ -159,8 +157,6 @@ | ||
var promises = [ | ||
asyncFn1() // foo | ||
asyncFn1(), // foo | ||
asyncFn2() // bar | ||
asyncFn3() // hello | ||
asyncFn4() // world | ||
] | ||
console.log(yield Promise.all(promises)) // ['foo', 'bar', 'hello', 'world'] | ||
console.log(yield Promise.all(promises)) // ['foo', 'bar'] | ||
@@ -171,8 +167,6 @@ // callback-all | ||
asyncFn2(all()) // bar | ||
asyncFn3(all()) // hello | ||
asyncFn4(all()) // world | ||
console.log(yield all(next)) // ['foo', 'bar', 'hello', 'world'] | ||
console.log(yield all(next)) // ['foo', 'bar'] | ||
})(function (err, res) { | ||
// handle error or return | ||
}).catch(function (err) { | ||
// handle uncaught error | ||
}) | ||
@@ -179,0 +173,0 @@ ``` |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
13152
288
169