Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open Chain #1

Merged
merged 2 commits into from
Mar 29, 2012
Merged

Open Chain #1

merged 2 commits into from
Mar 29, 2012

Conversation

schloerke
Copy link
Contributor

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:

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

alexkwolfe added a commit that referenced this pull request Mar 29, 2012
@alexkwolfe alexkwolfe merged commit e761168 into alexkwolfe:master Mar 29, 2012
@alexkwolfe
Copy link
Owner

Thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants