From 62e87045af76b334b135e80fdd7a21a8b2f25a72 Mon Sep 17 00:00:00 2001 From: Graeme Yeates Date: Sat, 29 Aug 2015 15:29:34 -0400 Subject: Implement eachOf through _keyIterator --- lib/async.js | 33 +++++++++++++++------------------ 1 file changed, 15 insertions(+), 18 deletions(-) (limited to 'lib') diff --git a/lib/async.js b/lib/async.js index 1f1e0ef..a7743f5 100644 --- a/lib/async.js +++ b/lib/async.js @@ -77,12 +77,6 @@ ); } - function _each(coll, iterator) { - return _isArrayLike(coll) ? - _arrayEach(coll, iterator) : - _forEachOf(coll, iterator); - } - function _arrayEach(arr, iterator) { var index = -1, length = arr.length; @@ -230,23 +224,26 @@ async.eachOf = function (object, iterator, callback) { callback = _once(callback || noop); object = object || []; - var size = _isArrayLike(object) ? object.length : _keys(object).length; - var completed = 0; - if (!size) { - return callback(null); - } - _each(object, function (value, key) { + + var iter = _keyIterator(object); + var key, completed = 0; + + while ((key = iter()) != null) { + completed += 1; iterator(object[key], key, only_once(done)); - }); + } + + if (completed === 0) callback(null); + function done(err) { + completed--; if (err) { callback(err); } - else { - completed += 1; - if (completed >= size) { - callback(null); - } + // Check key is null in case iterator isn't exhausted + // and done resolved synchronously. + else if (key === null && completed <= 0) { + callback(null); } } }; -- cgit v1.2.1