alexkwolfe/groupie — pull request #1 by schloerke (2012-03-13 23:50:54+00:00)
Title: Open Chain

Ran into a situation where asynchronous functions added to a open groupie.chain are called excessively.

The "add()" method was calling "callNext()" if the group "hasMore()" functions to process. Since this was always true after adding a new function, "callNext()" was executed. In the example below, the first function has not returned yet (incrementing 'pos'), so it is called 3 more times.

I'm thinking the logic within the "add()" method is trying to check if it is processing anything at the current time. If it is processing something, "callNext()" will be called when the processing function is done, otherwise start the new function with "callNext()".

All of this only comes into play when it's an open groupie.chain and a function is asynchronous.

Example code:
```javascript
group = groupie.chain(function(err, results) {
if (err != null) {
console.log("Error: ", err);
} else {
console.log("Results: ", results);
}
});

add_num = function(num, time) {
group.add(function(done) {
setTimeout(function() {
console.log("Done " + num + " - " + (new Date()).toISOString());
return done(null, num);
}, time);
});
};

add_num(1, 3000);
add_num(2, 2000);
add_num(3, 1000);
add_num(4, 4000);

group.finalize();
```

Output with current groupie:
```
Done 1 - 2012-03-13T23:38:00.130Z
Done 1 - 2012-03-13T23:38:00.132Z
Done 1 - 2012-03-13T23:38:00.132Z
Done 1 - 2012-03-13T23:38:00.132Z
Results: [ 1, 1, 1, 1 ]
Done 3 - 2012-03-13T23:38:01.132Z
Results: [ 1, 1, 1, 1, 3 ]
Done 2 - 2012-03-13T23:38:02.131Z
Results: [ 1, 1, 1, 1, 3, 2 ]
Done 4 - 2012-03-13T23:38:04.131Z
Results: [ 1, 1, 1, 1, 3, 2, 4 ]
```

Output with pull request:
```
Done 1 - 2012-03-13T23:36:50.348Z
Done 2 - 2012-03-13T23:36:52.349Z
Done 3 - 2012-03-13T23:36:53.350Z
Done 4 - 2012-03-13T23:36:57.351Z
Results: [ 1, 2, 3, 4 ]
```

Best,
Barret