diff options
Diffstat (limited to 'lib/eachOf.js')
-rw-r--r-- | lib/eachOf.js | 16 |
1 files changed, 10 insertions, 6 deletions
diff --git a/lib/eachOf.js b/lib/eachOf.js index 070204b..0b847ae 100644 --- a/lib/eachOf.js +++ b/lib/eachOf.js @@ -1,15 +1,14 @@ import isArrayLike from './internal/isArrayLike'; import breakLoop from './internal/breakLoop'; import eachOfLimit from './eachOfLimit'; -import doLimit from './internal/doLimit'; -import noop from './internal/noop'; import once from './internal/once'; import onlyOnce from './internal/onlyOnce'; import wrapAsync from './internal/wrapAsync'; +import awaitify from './internal/awaitify' // eachOf implementation optimized for array-likes function eachOfArrayLike(coll, iteratee, callback) { - callback = once(callback || noop); + callback = once(callback); var index = 0, completed = 0, {length} = coll, @@ -36,7 +35,9 @@ function eachOfArrayLike(coll, iteratee, callback) { } // a generic version of eachOf which can handle array, object, and iterator cases. -var eachOfGeneric = doLimit(eachOfLimit, Infinity); +function eachOfGeneric (coll, iteratee, callback) { + return eachOfLimit(coll, Infinity, iteratee, callback); +} /** * Like [`each`]{@link module:Collections.each}, except that it passes the key (or index) as the second argument @@ -56,6 +57,7 @@ var eachOfGeneric = doLimit(eachOfLimit, Infinity); * Invoked with (item, key, callback). * @param {Function} [callback] - A callback which is called when all * `iteratee` functions have finished, or an error occurs. Invoked with (err). + * @returns {Promise} a promise, if a callback is omitted * @example * * var obj = {dev: "/dev.json", test: "/test.json", prod: "/prod.json"}; @@ -77,7 +79,9 @@ var eachOfGeneric = doLimit(eachOfLimit, Infinity); * doSomethingWith(configs); * }); */ -export default function(coll, iteratee, callback) { +function eachOf(coll, iteratee, callback) { var eachOfImplementation = isArrayLike(coll) ? eachOfArrayLike : eachOfGeneric; - eachOfImplementation(coll, wrapAsync(iteratee), callback); + return eachOfImplementation(coll, wrapAsync(iteratee), callback); } + +export default awaitify(eachOf, 3) |