Last active
June 14, 2018 09:49
-
-
Save tuor4eg/0eef4954d10faf75a8cca4551d4a01b5 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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