Socket
Socket
Sign inDemoInstall

co

Package Overview
Dependencies
0
Maintainers
1
Versions
34
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

Comparing version 2.2.0 to 2.3.0

test/arrays.js

34

benchmark.js

@@ -11,3 +11,3 @@

function *gen() {
}

@@ -23,4 +23,4 @@

suite('co()', function(){
set('mintime', 1000)
set('mintime', process.env.MINTIME | 0 || 1)
bench('promises', function(done){

@@ -42,3 +42,3 @@ co(function *(){

bench('thunk join', function(done){
bench('arrays', function(done){
co(function *(){

@@ -49,2 +49,28 @@ yield [fun, fun, fun];

bench('objects', function(done){
co(function *(){
yield {
a: fun,
b: fun,
c: fun
};
})(done);
})
bench('generators', function(done){
co(function *(){
yield gen();
yield gen();
yield gen();
})(done);
})
bench('generators delegated', function(done){
co(function *(){
yield* gen();
yield* gen();
yield* gen();
})(done);
})
bench('generator functions', function(done){

@@ -51,0 +77,0 @@ co(function *(){

2.3.0 / 2013-11-12
==================
* add `yield object` support
2.2.0 / 2013-11-05

@@ -3,0 +8,0 @@ ==================

@@ -0,6 +1,12 @@

/**
* toString reference.
* toString() reference.
*/
var toString = Object.prototype.toString;
/**
* slice() reference.
*/
var slice = Array.prototype.slice;

@@ -12,3 +18,3 @@

exports = module.exports = co;
module.exports = co;

@@ -79,3 +85,3 @@ /**

// invalid
next(new Error('yield a function, promise, generator, or array'));
next(new Error('yield a function, promise, generator, array, or object'));
}

@@ -92,11 +98,29 @@

/**
* Join the given `fns`.
* Convert `obj` into a normalized thunk.
*
* @param {Array|Function} ...
* @param {Mixed} obj
* @param {Mixed} ctx
* @return {Function}
* @api public
* @api private
*/
exports.join = function(fns) {
if (!Array.isArray(fns)) fns = slice.call(arguments);
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.call(ctx, obj);
if (isPromise(obj)) fn = promiseToThunk(obj);
return fn;
}
/**
* Convert an array of yieldables to a thunk.
*
* @param {Array}
* @return {Function}
* @api private
*/
function arrayToThunk(fns) {
var ctx = this;

@@ -142,9 +166,8 @@

}
};
}
/**
* Convert `obj` into a normalized thunk.
* Convert an object of yieldables to a thunk.
*
* @param {Mixed} obj
* @param {Mixed} ctx
* @param {Object} obj
* @return {Function}

@@ -154,9 +177,44 @@ * @api private

function toThunk(obj, ctx) {
var fn = obj;
if (Array.isArray(obj)) fn = exports.join.call(ctx, obj);
if (isGeneratorFunction(obj)) obj = obj.call(ctx);
if (isGenerator(obj)) fn = co.call(ctx, obj);
if (isPromise(obj)) fn = promiseToThunk(obj);
return fn;
function objectToThunk(obj){
var ctx = this;
return function(done){
var keys = Object.keys(obj);
var pending = keys.length;
var results = {};
var finished;
if (!pending) {
setImmediate(function(){
done(null, results)
});
return;
}
for (var i = 0; i < keys.length; i++) {
run(obj[keys[i]], keys[i]);
}
function run(fn, key) {
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[key] = res;
--pending || done(null, results);
});
} catch (err) {
finished = true;
done(err);
}
}
}
}

@@ -163,0 +221,0 @@

5

package.json
{
"name": "co",
"version": "2.2.0",
"version": "2.3.0",
"description": "generator async flow control goodness",

@@ -20,2 +20,5 @@ "keywords": [

},
"scripts": {
"test": "make test"
},
"license": "MIT",

@@ -22,0 +25,0 @@ "repository": {

@@ -21,3 +21,3 @@

})
describe('with no yields', function(){

@@ -204,3 +204,3 @@ it('should work', function(done){

var msg = 'yield a function, promise, generator, or array';
var msg = 'yield a function, promise, generator, array, or object';
errors.should.eql([msg, msg]);

@@ -207,0 +207,0 @@ })(done);

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc