forEachLimit does not work as expected #203

Closed
daviddavid opened this Issue Nov 27, 2012 · 2 comments

Projects

None yet

3 participants

@daviddavid
forEachLimit(arr, limit, iterator, callback)

The same as forEach only the iterator is applied to batches of items in the array, in series. The next batch of iterators is only called once the current one has completed processing.

This is not true. For example, we have an ordered list [1,2,...,6] and let forEachLimit iterate each int in a batch of 2, while taking a random amount of time for each iteration. The int is then pushed to an array. According to the description, the result should always be in the same groups of 2 (ie. [1,2] , [3,4] , [5,6]). However, this is not the case:

var arr = [1,2,3,4,5,6];
var limit = 2;
var result = [];

function iterator(item, callback) {
  var waitTime = Math.floor(Math.random() * 1000);

  setTimeout(function() {
    result.push(item);
    callback();
  }, waitTime);
};

async.forEachLimit(arr, limit, iterator, function(err) {
  console.log(result);
});

Sample result: [ 2, 3, 4, 1, 6, 5 ]

@dougwilson
Contributor

I think forEachLimit works as people would want, but the docs were contributed later. I believe the docs are in the wrong here and they should be fixed rather than changing how forEachLimit functions.

@brianmaissy brianmaissy added a commit to brianmaissy/async that referenced this issue Feb 5, 2013
@brianmaissy brianmaissy corrected the documentation of forEachLimit and mapLimit: the items a…
…re not processed in batches. Addresses issue #203
b62eff0
@caolan
Owner
caolan commented Feb 6, 2013

I agree with @dougwilson, the docs were in the wrong in this case. Thanks for the pull request @brianmaissy

@caolan caolan closed this Feb 6, 2013
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment