Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

allow forEach to complete even if it's array grows.

  • Loading branch information...
commit 3eda64242c5557ce2f4ee9809a0f7a8fe9af4473 1 parent 8696792
@rektide authored
Showing with 19 additions and 3 deletions.
  1. +4 −3 lib/async.js
  2. +15 −0 test/test-async.js
View
7 lib/async.js
@@ -82,7 +82,8 @@
return callback();
}
var completed = 0;
- _forEach(arr, function (x) {
+ for(var i = 0; i < arr.length; ++i){
+ var x= arr[i];
iterator(x, function (err) {
if (err) {
callback(err);
@@ -90,12 +91,12 @@
}
else {
completed += 1;
- if (completed === arr.length) {
+ if (completed >= arr.length) {
callback(null);
}
}
});
- });
+ }
};
async.forEachSeries = function (arr, iterator, callback) {
View
15 test/test-async.js
@@ -528,6 +528,21 @@ exports['forEach no callback'] = function(test){
async.forEach([1], forEachNoCallbackIterator.bind(this, test));
};
+exports['forEach with growing array'] = function(test){
+ var arr= [2,3];
+ var iterations = 0;
+ async.forEach(arr, function(x, callback){
+ if (iterations++ == 0) {
+ arr.push(4);
+ }
+ callback();
+ }, function(){
+ test.same(arr, [2,3,4]);
+ test.same(iterations, 3);
+ test.done();
+ })
+};
+
exports['forEachSeries'] = function(test){
var args = [];
async.forEachSeries([1,3,2], forEachIterator.bind(this, args), function(err){
Please sign in to comment.
Something went wrong with that request. Please try again.