Skip to content

Instantly share code, notes, and snippets.

@tuor4eg
Last active June 14, 2018 09:49
Show Gist options
  • Save tuor4eg/0eef4954d10faf75a8cca4551d4a01b5 to your computer and use it in GitHub Desktop.
Save tuor4eg/0eef4954d10faf75a8cca4551d4a01b5 to your computer and use it in GitHub Desktop.
const noop = () => {};
const once = (fn) => {
let called = false;
return (...args) => {
if (called) return;
called = true;
fn(...args);
};
};
const each = (coll, iteratee, callback = noop) => {
const oncedCallback = once(callback);
if (coll.length === 0) {
callback(null);
return;
}
let completed = 0;
const cbEach = err => {
if (err) {
oncedCallback(err);
return;
}
completed++;
if (completed === coll.length) {
oncedCallback(null);
}
};
coll.forEach(item => iteratee(item, cbEach));
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment