-
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
Closed
Closed
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
710cc8c
Implemented a way to pass a key argument to the iterator for async.each
olalonde df106f5
Updated README.md with usage of async.each with a key
olalonde 180183d
Fixed bug: cannot use arr.length with objects
olalonde ba4d138
Implemented eachSeries with optional key argument
olalonde 32db182
Removed console.log and fire callback immediately when object has no …
olalonde File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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(); | ||
}; | ||
|
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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?