An array which grows never completed a forEach #158

Closed
wants to merge 1 commit into
from
View
@@ -76,22 +76,33 @@
async.nextTick = process.nextTick;
}
- async.forEach = function (arr, iterator, callback) {
+ async.forEach = function (arr, iterator, callback, skip) {
callback = callback || function () {};
+ skip = skip || 0;
if (!arr.length) {
return callback();
}
- var completed = 0;
+ var completed = skip;
+ var initialLength = arr.length;
_forEach(arr, function (x) {
+ if (skip > 0) {
+ --skip;
+ return;
+ }
iterator(x, function (err) {
if (err) {
callback(err);
callback = function () {};
}
else {
completed += 1;
- if (completed === arr.length) {
- callback(null);
+ if (completed === initialLength) {
+ if (arr.length <= initialLength) {
+ callback(null);
+ }
+ else {
+ async.forEach(arr, iterator, callback, completed);
+ }
}
}
});
View
@@ -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){