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

forEach with object + key in iterator #72

Closed
Bartvds opened this issue Dec 15, 2011 · 4 comments
Closed

forEach with object + key in iterator #72

Bartvds opened this issue Dec 15, 2011 · 4 comments

Comments

@Bartvds
Copy link

Bartvds commented Dec 15, 2011

I was editing some code I had written synchronously with underscore.js (aren't we all :), and wanted to swap sync stuff out for some async goodness. But I found a conceptual mismatch: Underscore's ._each() has a function(value, key, list) iterator format, async only has a (value, callback): it's 'missing' the key +list.

Having the hash, key, value inside the iterator is very useful if you need a bit more then just a basic map()-like. Right now the iterator has no way of knowing where in the input collection the current value would fit or what it's key was. Which is fine when you just update the same collection (map() etc), but when you need the key for something else beside the looping it get's ackward.

Now I do a _.keys() and pull that through some other async stuff. It's nearly the same and works but it's not in the spirit of things no? :)

(edit: if I gathered enough courage I might attempt a fork/edit/pull but no promises :)

@caolan
Copy link
Owner

caolan commented Dec 15, 2011

Yes, the reason being I didn't want to require each iterator to accept
the arguments: function (value, key, list, callback), since you
couldn't then just drop in some useful node.js functions. I
experimented with some variable callback arguments in nimble
(https://github.com/caolan/nimble), which allows you to iterate over
object properties in the way you describe but also allow you to use
the shorter syntax.

This is something I'd like to support in async in the future, but I
need to find a way that won't stop a lot of the standard node.js
library being useful as iterators.

@Bartvds
Copy link
Author

Bartvds commented Dec 16, 2011

You could start a new 'postfix', like a forEachAt(), or just eachAt() or something. Whole point for this one would be to have that list + key + value in the iterator so it sort of specifically would break pattern, and and this could indicate it maybe?

@olalonde
Copy link

+1. I really need my keys :) Any plans to implement this? I could write a pull request.

@elad
Copy link

elad commented Jun 1, 2014

I would also like this functionality. I think @Bartvds's way is least intrusive, but maybe use the Key postfix, like eachKey() and so on.

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

No branches or pull requests

4 participants