-
Notifications
You must be signed in to change notification settings - Fork 2.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Pass key to iterator if async.each() iterator takes 3 arguments #321
Changes from all commits
710cc8c
df106f5
180183d
ba4d138
32db182
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -28,6 +28,13 @@ | |
//// cross-browser compatiblity functions //// | ||
|
||
var _each = function (arr, iterator) { | ||
if (!(arr instanceof Array) && typeof arr === 'object') { | ||
for (var key in arr) { | ||
if (Object.prototype.hasOwnProperty.call(arr, key)) | ||
iterator(arr[key], key, arr); | ||
} | ||
return; | ||
} | ||
if (arr.forEach) { | ||
return arr.forEach(iterator); | ||
} | ||
|
@@ -70,6 +77,12 @@ | |
return keys; | ||
}; | ||
|
||
var _length = function (arr) { | ||
if (arr instanceof Array) return arr.length; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. instanceof Array is not reliable. you should use Array.isArray(), or Object.prototype.toString.call(arr) === '[object Array]' |
||
if (Object.keys) return Object.keys(arr).length; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. doesn't _keys handle using Object.keys if it's available? |
||
return _keys(arr).length; | ||
} | ||
|
||
//// exported async module functions //// | ||
|
||
//// nextTick implementation with browser-compatible fallback //// | ||
|
@@ -97,49 +110,75 @@ | |
|
||
async.each = function (arr, iterator, callback) { | ||
callback = callback || function () {}; | ||
if (!arr.length) { | ||
if (arr instanceof Array && !arr.length) { | ||
return callback(); | ||
} | ||
|
||
var completed = 0; | ||
_each(arr, function (x) { | ||
iterator(x, only_once(function (err) { | ||
var withKey = (iterator.length === 3); | ||
|
||
_each(arr, function (x, key) { | ||
var args = []; | ||
args.push(x); | ||
if (withKey) | ||
args.push(key); | ||
args.push(only_once(function (err) { | ||
if (err) { | ||
callback(err); | ||
callback = function () {}; | ||
} | ||
else { | ||
completed += 1; | ||
if (completed >= arr.length) { | ||
if (completed >= _length(arr)) { | ||
callback(null); | ||
} | ||
} | ||
})); | ||
|
||
iterator.apply(iterator, args); | ||
}); | ||
}; | ||
async.forEach = async.each; | ||
|
||
async.eachSeries = function (arr, iterator, callback) { | ||
callback = callback || function () {}; | ||
if (!arr.length) { | ||
if (arr instanceof Array && !arr.length) { | ||
return callback(); | ||
} | ||
if (!(arr instanceof Array)) { | ||
var keys = _keys(arr); | ||
if (!keys.length) | ||
return callback(); | ||
} | ||
|
||
var completed = 0; | ||
var withKey = (iterator.length === 3); | ||
|
||
var iterate = function () { | ||
iterator(arr[completed], function (err) { | ||
var args = []; | ||
var key = (arr instanceof Array) ? completed : keys[completed]; | ||
var val = arr[key]; | ||
|
||
args.push(val); | ||
if (withKey) { | ||
args.push(key); | ||
} | ||
args.push(function (err) { | ||
if (err) { | ||
callback(err); | ||
callback = function () {}; | ||
} | ||
else { | ||
completed += 1; | ||
if (completed >= arr.length) { | ||
if (completed >= _length(arr)) { | ||
callback(null); | ||
} | ||
else { | ||
iterate(); | ||
} | ||
} | ||
}); | ||
iterator.apply(iterator, args); | ||
}; | ||
iterate(); | ||
}; | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
what about the context? iterator.call(context, …) where
context
is either "this", or arguments[2] if arguments.length > 2?