summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlexander Early <aearly@fluid.com>2015-05-30 20:43:05 -0700
committerAlexander Early <aearly@fluid.com>2015-05-30 20:43:05 -0700
commitacf0ccb571ac94ed7acbd7c0a9d1f95f3cfe6334 (patch)
tree43f95415011faa057870b381917852533a256455
parent8d55ac686f9681e502f25b94b84c37359af3b746 (diff)
downloadasync-acf0ccb571ac94ed7acbd7c0a9d1f95f3cfe6334.tar.gz
redefined each in terms of eachOf
-rw-r--r--lib/async.js66
1 files changed, 19 insertions, 47 deletions
diff --git a/lib/async.js b/lib/async.js
index f863530..8f3903d 100644
--- a/lib/async.js
+++ b/lib/async.js
@@ -107,6 +107,13 @@
return keys;
};
+ function _iteratorKeys(coll) {
+ return _isArrayLike(coll) ?
+ // just plain _keys wont work with sparse arrays
+ _map(coll, function (_, index) { return index; }) :
+ _keys(coll);
+ }
+
function _baseSlice(arr, start) {
start = start || 0;
var index = -1;
@@ -124,6 +131,12 @@
return result;
}
+ function _withoutIndex(iterator) {
+ return function (value, index, callback) {
+ return iterator(value, callback);
+ };
+ }
+
//// exported async module functions ////
//// nextTick implementation with browser-compatible fallback ////
@@ -164,59 +177,18 @@
async.forEach =
async.each = function (arr, iterator, callback) {
- callback = callback || noop;
- if (!arr.length) {
- return callback(null);
- }
- var completed = 0;
- _arrayEach(arr, function (x) {
- iterator(x, only_once(done) );
- });
- function done(err) {
- if (err) {
- callback(err);
- callback = noop;
- }
- else {
- completed += 1;
- if (completed >= arr.length) {
- callback(null);
- }
- }
- }
+ return async.eachOf(arr, _withoutIndex(iterator), callback);
};
async.forEachSeries =
async.eachSeries = function (arr, iterator, callback) {
- callback = callback || noop;
- if (!arr.length) {
- return callback(null);
- }
- var completed = 0;
- function iterate() {
- iterator(arr[completed], function (err) {
- if (err) {
- callback(err);
- callback = noop;
- }
- else {
- completed += 1;
- if (completed >= arr.length) {
- callback(null);
- }
- else {
- iterate();
- }
- }
- });
- }
- iterate();
+ return async.eachOfSeries(arr, _withoutIndex(iterator), callback);
};
async.forEachLimit =
async.eachLimit = function (arr, limit, iterator, callback) {
- var fn = _eachLimit(limit)(arr, iterator, callback);
+ var fn = _eachOfLimit(limit)(arr, _withoutIndex(iterator), callback);
};
function _eachLimit(limit) {
@@ -271,7 +243,7 @@
if (!size) {
return callback(null);
}
- _forEachOf(object, function (value, key) {
+ _each(object, function (value, key) {
iterator(object[key], key, only_once(done));
});
function done(err) {
@@ -291,7 +263,7 @@
async.forEachOfSeries =
async.eachOfSeries = function (obj, iterator, callback) {
callback = callback || noop;
- var keys = _keys(obj);
+ var keys = _iteratorKeys(obj);
var size = keys.length;
if (!size) {
return callback();
@@ -336,7 +308,7 @@
return function (obj, iterator, callback) {
callback = callback || noop;
- var keys = _keys(obj);
+ var keys = _iteratorKeys(obj);
var size = keys.length;
if (!size || limit <= 0) {
return callback(null);