Skip to content

Commit

Permalink
fix "RangeError: Maximum call stack size exceeded"
Browse files Browse the repository at this point in the history
  • Loading branch information
smbape committed Feb 14, 2018
1 parent d028078 commit cd6beba
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
5 changes: 4 additions & 1 deletion lib/internal/eachOfLimit.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export default function _eachOfLimit(limit) {
var nextElem = iterator(obj);
var done = false;
var running = 0;
var looping = false;

function iterateeCallback(err, value) {
running -= 1;
Expand All @@ -26,12 +27,13 @@ export default function _eachOfLimit(limit) {
done = true;
return callback(null);
}
else {
else if (!looping) {
replenish();
}
}

function replenish () {
looping = true;
while (running < limit && !done) {
var elem = nextElem();
if (elem === null) {
Expand All @@ -44,6 +46,7 @@ export default function _eachOfLimit(limit) {
running += 1;
iteratee(elem.value, elem.key, onlyOnce(iterateeCallback));
}
looping = false;
}

replenish();
Expand Down
12 changes: 12 additions & 0 deletions mocha_test/eachOf.js
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,18 @@ describe("eachOf", function() {
setTimeout(done, 25);
});

it('forEachOfLimit 1024 * 1024 synchronous call', function(done) {
var count = 0;
async.forEachOfLimit(_.range(1024 * 1024), Infinity, function(x, i, callback){
count++;
callback();
}, function(err){
if (err) throw err;
expect(count).to.equal(1024 * 1024);
});
setTimeout(done, 25);
});

it('forEachOfLimit error', function(done) {
var obj = { a: 1, b: 2, c: 3, d: 4, e: 5 };
var call_order = [];
Expand Down

0 comments on commit cd6beba

Please sign in to comment.