Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Fixed a bug with empty array of functions.

  • Loading branch information...
commit 05f992e49fdd2a92e39d94c57b2303330f182fa5 1 parent c8250b5
@alexkwolfe authored
View
4 README.md
@@ -56,8 +56,8 @@ use the function callback as the Node callback. The above example could be rewri
### Your function results
-The results of your function calls is available in second parameter of the group or chain
-callback. The results is an array of the values submitted by your functions in the `done`
+The results of your function calls are available in second parameter of the group or chain
+callback. The results array of the values submitted by your functions in the `done`
callback, in the same order that the functions are declared.
If the `done` method is called with a `null` parameter or no parameter, then `null` or
View
32 lib/groupie.js
@@ -1,14 +1,6 @@
var sys = require('sys');
-exports.bind = function(scope) {
- var _function = this;
-
- return function() {
- return _function.apply(scope, arguments);
- }
-}
-
/**
* Executes a group of functions concurrently, invoking a callback when all have completed
* or when an error occurs. Errors occur when an executed function throws an unhandled
@@ -27,10 +19,12 @@ exports.bind = function(scope) {
exports.group = function () {
var args = Array.prototype.slice.call(arguments);
var cb = args.pop();
- var fxns = args.length > 0 && Array.isArray(args[0]) ? args.shift() : [];
- var open = !fxns.length;
+ var fxns = args.length > 0 && Array.isArray(args[0]) ? args.shift() : null;
+ var open = fxns === null;
var finalized = !open;
+ fxns = fxns || [];
+
var items_left_to_execute = fxns.length;
var results = [];
var errOccurred = false;
@@ -43,17 +37,23 @@ exports.group = function () {
}
results[i] = result;
items_left_to_execute--;
- if (finalized && !items_left_to_execute)
- cb(null, results)
+ if (finalized && !items_left_to_execute) {
+ cb(null, results);
+ }
};
try {
fxn(done);
} catch (err) {
+ sys.puts("ERR: " + err);
done(err, results);
}
};
+
+ if (fxns.length === 0 && !open)
+ cb(null, []);
+
for ( var i = 0; i < fxns.length; i++) {
callGroupFunction(i, fxns[i]);
}
@@ -81,9 +81,11 @@ exports.group = function () {
exports.chain = function () {
var args = Array.prototype.slice.call(arguments);
var cb = args.pop();
- var fxns = args.length > 0 && Array.isArray(args[0]) ? args.shift() : [];
- var open = !fxns.length;
+ var fxns = args.length > 0 && Array.isArray(args[0]) ? args.shift() : null;
+ var open = fxns === null;
var finalized = !open;
+
+ fxns = fxns || [];
var pos = 0;
var results = [];
@@ -113,6 +115,8 @@ exports.chain = function () {
if (fxns.length > 0)
callNext();
+ else if (!open)
+ cb(null, []);
if (open)
return {
View
2  package.json
@@ -1,6 +1,6 @@
{ "name" : "groupie"
, "description" : "A simple flow control library for node.js for executing multiple functions as a group or in a chain, calling back when all functions have finished."
-, "version" : "0.1.0"
+, "version" : "0.1.1"
, "homepage" : "http://github.com/alexkwolfe/groupie"
, "author" : "Alex Wolfe <alexkwolfe@gmail.com>"
, "contributors" : []
View
8 test/unit/chain_test.js
@@ -31,6 +31,14 @@ exports.testResults = function(assert) {
});
};
+exports.testNoFunctions = function(assert) {
+ groupie.chain([], function(err, colors) {
+ assert.ok(!err);
+ assert.same([], colors);
+ assert.done();
+ });
+};
+
exports.testNullResultsAreNotDiscarded = function(assert) {
assert.expect(1);
View
8 test/unit/group_test.js
@@ -35,6 +35,14 @@ exports.testResults = function(assert) {
});
};
+exports.testNoFunctions = function(assert) {
+ groupie.group([], function(err, colors) {
+ assert.ok(!err);
+ assert.same([], colors);
+ assert.done();
+ });
+};
+
exports.testNullResultsAreNotDiscarded = function(assert) {
assert.expect(1);
Please sign in to comment.
Something went wrong with that request. Please try again.