Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Minor cleanups to eachOfLimit #1245

Merged
merged 1 commit into from
Jul 21, 2016
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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();
};
}