Comparing version 3.0.2 to 3.0.4
{ | ||
"name": "co", | ||
"version": "3.0.2", | ||
"version": "3.0.3", | ||
"description": "generator async flow control goodness", | ||
@@ -5,0 +5,0 @@ "keywords": [ |
@@ -13,3 +13,3 @@ | ||
// for an alternative see ./join.js | ||
// for an alternative see ./array.js | ||
// also if writing all this boilerplate | ||
@@ -16,0 +16,0 @@ // is not to your liking check out "thunkify" |
@@ -0,1 +1,12 @@ | ||
3.0.4 / 2014-02-17 | ||
================== | ||
* fix toThunk object check regression. Closes #89 | ||
3.0.3 / 2014-02-08 | ||
================== | ||
* refactor: arrayToThunk @AutoSponge #88 | ||
3.0.2 / 2014-01-01 | ||
@@ -2,0 +13,0 @@ ================== |
89
index.js
@@ -39,14 +39,8 @@ /** | ||
// we only need to parse the arguments | ||
// if gen is a generator function. | ||
if (isGenFun) { | ||
// we only need to parse the arguments | ||
// if gen is a generator function. | ||
var args = slice.call(arguments); | ||
// no callback provided | ||
if (!args.length) done = error; | ||
// callback is the last argument | ||
else if ('function' == typeof args[args.length - 1]) done = args.pop(); | ||
// arguments provided, but no callbacks | ||
else done = error; | ||
var args = slice.call(arguments), len = args.length; | ||
var hasCallback = len && 'function' == typeof args[len - 1]; | ||
done = hasCallback ? args.pop() : error; | ||
gen = fn.apply(this, args); | ||
@@ -124,60 +118,27 @@ } else { | ||
function toThunk(obj, ctx) { | ||
var fn = obj; | ||
if (Array.isArray(obj)) fn = arrayToThunk.call(ctx, obj); | ||
if ('[object Object]' == toString.call(obj)) fn = objectToThunk.call(ctx, obj); | ||
if (isGeneratorFunction(obj)) obj = obj.call(ctx); | ||
if (isGenerator(obj)) fn = co(obj); | ||
if (isPromise(obj)) fn = promiseToThunk(obj); | ||
return fn; | ||
} | ||
if (Array.isArray(obj)) { | ||
return objectToThunk.call(ctx, obj); | ||
} | ||
/** | ||
* Convert an array of yieldables to a thunk. | ||
* | ||
* @param {Array} | ||
* @return {Function} | ||
* @api private | ||
*/ | ||
if (isGeneratorFunction(obj)) { | ||
return co(obj.call(ctx)); | ||
} | ||
function arrayToThunk(fns) { | ||
var ctx = this; | ||
if (isGenerator(obj)) { | ||
return co(obj); | ||
} | ||
return function(done){ | ||
var pending = fns.length; | ||
var results = new Array(pending); | ||
var finished; | ||
if (isPromise(obj)) { | ||
return promiseToThunk(obj); | ||
} | ||
if (!pending) { | ||
setImmediate(function(){ | ||
done(null, results); | ||
}); | ||
return; | ||
} | ||
if ('function' == typeof obj) { | ||
return obj; | ||
} | ||
for (var i = 0; i < fns.length; i++) { | ||
run(fns[i], i); | ||
} | ||
if (obj && 'object' == typeof obj) { | ||
return objectToThunk.call(ctx, obj); | ||
} | ||
function run(fn, i) { | ||
if (finished) return; | ||
try { | ||
fn = toThunk(fn, ctx); | ||
fn.call(ctx, function(err, res){ | ||
if (finished) return; | ||
if (err) { | ||
finished = true; | ||
return done(err); | ||
} | ||
results[i] = res; | ||
--pending || done(null, results); | ||
}); | ||
} catch (err) { | ||
finished = true; | ||
done(err); | ||
} | ||
} | ||
} | ||
return obj; | ||
} | ||
@@ -199,3 +160,3 @@ | ||
var pending = keys.length; | ||
var results = {}; | ||
var results = new obj.constructor(); | ||
var finished; | ||
@@ -202,0 +163,0 @@ |
{ | ||
"name": "co", | ||
"version": "3.0.2", | ||
"version": "3.0.4", | ||
"description": "generator async flow control goodness", | ||
@@ -5,0 +5,0 @@ "keywords": [ |
# Co | ||
[![Build Status](https://travis-ci.org/visionmedia/co.png)](https://travis-ci.org/visionmedia/co) | ||
[![Build Status](https://travis-ci.org/visionmedia/co.png?branch=master)](https://travis-ci.org/visionmedia/co) | ||
@@ -10,3 +10,4 @@ Generator based flow-control goodness for nodejs (and soon the browser), using | ||
Currently you must use the `--harmony-generators` flag when | ||
running node 0.11.x to get access to generators. | ||
running node 0.11.x to get access to generators. Or use gnode to spawn your node instance. | ||
However note that performance degrades quickly compared to 0.11.x. | ||
@@ -34,2 +35,5 @@ Co is careful to relay any errors that occur back to the generator, including those | ||
var co = require('co'); | ||
var thunkify = require('thunkify'); | ||
var request = require('request'); | ||
var get = thunkify(request.get); | ||
@@ -234,3 +238,3 @@ co(function *(){ | ||
Or using join: | ||
Or: | ||
@@ -266,3 +270,2 @@ ```js | ||
var co = require('co'); | ||
var join = co.join; | ||
var fs = require('fs'); | ||
@@ -283,15 +286,2 @@ | ||
var c = size('Makefile'); | ||
var res = yield join(a, b, c); | ||
console.log(res); | ||
// => [ 13, 1687, 129 ] | ||
})() | ||
``` | ||
As an alias of `join(array)` you may simply `yield` an array: | ||
```js | ||
co(function *(){ | ||
var a = size('.gitignore'); | ||
var b = size('index.js'); | ||
var c = size('Makefile'); | ||
var res = yield [a, b, c]; | ||
@@ -303,3 +293,3 @@ console.log(res); | ||
Nested joins may also be expressed as simple nested arrays: | ||
Nested arrays may also be expressed as simple nested arrays: | ||
@@ -306,0 +296,0 @@ ```js |
Sorry, the diff of this file is not supported yet
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
40785
1182
344