Skip to content

Commit

Permalink
Merge pull request #1245 from caolan/each-cleanup
Browse files Browse the repository at this point in the history
Minor cleanups to eachOfLimit
  • Loading branch information
megawac committed Jul 21, 2016
2 parents ddef433 + 5cea59d commit 72a7810
Showing 1 changed file with 19 additions and 19 deletions.
38 changes: 19 additions & 19 deletions lib/internal/eachOfLimit.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,29 @@ import onlyOnce from './onlyOnce';
export default function _eachOfLimit(limit) {
return function (obj, iteratee, callback) {
callback = once(callback || noop);
obj = obj || [];
var nextElem = iterator(obj);
if (limit <= 0) {
if (limit <= 0 || !obj) {
return callback(null);
}
var nextElem = iterator(obj);
var done = false;
var running = 0;
var errored = false;

(function replenish () {
if (done && running <= 0) {
function iterateeCallback(err) {
running -= 1;
if (err) {
done = true;
callback(err);
}
else if (done && running <= 0) {
return callback(null);
}
else {
replenish();
}
}

while (running < limit && !errored) {
function replenish () {
while (running < limit && !done) {
var elem = nextElem();
if (elem === null) {
done = true;
Expand All @@ -31,18 +39,10 @@ export default function _eachOfLimit(limit) {
return;
}
running += 1;
/* eslint {no-loop-func: 0} */
iteratee(elem.value, elem.key, onlyOnce(function (err) {
running -= 1;
if (err) {
callback(err);
errored = true;
}
else {
replenish();
}
}));
iteratee(elem.value, elem.key, onlyOnce(iterateeCallback));
}
})();
}

replenish();
};
}

0 comments on commit 72a7810

Please sign in to comment.