Skip to content
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
wants to merge 5 commits into from

Conversation

olalonde
Copy link

I have implemented a way to optionally pass a key/index argument to the async.each iterator.

async.each([1,2,3], function (val, index, callback) {});
async.each({a: 1, b: 2, c: 3}, function (val, key, callback) {});

It is backwards compatible since it omits the key/index argument for iterator functions that accept less than 3 arguments.

All tests pass.

See issue #72.

@@ -70,6 +77,12 @@
return keys;
};

var _length = function (arr) {
if (arr instanceof Array) return arr.length;
Copy link

Choose a reason for hiding this comment

The 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]'

@gobwas
Copy link

gobwas commented Nov 25, 2013

What about this feature? =)

@ljharb
Copy link

ljharb commented Nov 25, 2013

Still waiting for updates from @olalonde :-)

@caolan
Copy link
Owner

caolan commented Mar 28, 2014

Async does not use function arity to alter behaviour, since it can be unreliable in some cases.

@caolan caolan closed this Mar 28, 2014
@ljharb
Copy link

ljharb commented Mar 28, 2014

@caolan when can function arity be unreliable? jQuery uses this technique, I believe.

@aearly
Copy link
Collaborator

aearly commented Mar 28, 2014

fs.readFile(filename, [encoding], callback) (encoding is optional)

If you did something like async.each({file1: "file1", file2: "file2"}, fs.readFile, callback) it would break with your patch because it would pass "file1" as the encoding.

There is a PR open to add a forEachOf method (#168) that adds a separate method with the behaviour you want, it just needs docs and tests.

@caolan
Copy link
Owner

caolan commented Mar 30, 2014

What @aearly said :)

@gobwas
Copy link

gobwas commented Dec 1, 2014

@aearly 's example will also be broken with the native forEach method:

["fileA.txt", "fileB.txt"].forEach(fs.readFileSync);

Cause the native will pass 0, 1 as the encodings.

I mean, the example with the fs interface is not giving us any reason to do not pass the index/key in the handler.

@aearly
Copy link
Collaborator

aearly commented Dec 1, 2014

@gobwas You're right about how the native forEach works, but your reasoning is wrong. The native forEach fails with fs.readFileSync for the exact same reason async.each would fail with fs.readFile if you passed the index.

The way async.each works now works with fs.readFile with no modifications. The proposed change breaks that.

@gobwas
Copy link

gobwas commented Dec 2, 2014

@aearly yes, exactly, it will fall in the same reason. And thats why we can
ask ourselfs a question - does native forEach also bad designed with
passing index as second argument? :)

@gobwas
Copy link

gobwas commented Dec 2, 2014

@aearly Was reading your comment from mobile, and not catched the last sentence. I agree that this feature breaks backward compatibility. But, async now at version 0.9.0, which means, by semver, that it is still not stable. This is a technical question, how to minimize backward compatibility issues. I think the interface of native iterators could be the leading idea in this question.

@aearly
Copy link
Collaborator

aearly commented Dec 2, 2014

#168 addresses your concerns -- it would be backwards compatible since it introduces a new method.

While async is technically 0.x which is "the wild west" as far as semver is concerned, async is still used by thousands of projects and a change like what you propose would break tons of people's apps. (Some people have argued that async is ready for 1.0, though: see #562.)

@gobwas
Copy link

gobwas commented Dec 3, 2014

@aearly Mostly I agree with you. But. When you will release 2.0 version of async, will it be compatible to the current version? And will tons of people's apps will be broken of it?

@aearly aearly mentioned this pull request May 19, 2015
aearly added a commit that referenced this pull request May 19, 2015
Completing forEachOf.  Also closes #168 and #321
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants