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

Allow to use generators #579

Closed
piranna opened this issue Jul 9, 2014 · 15 comments
Closed

Allow to use generators #579

piranna opened this issue Jul 9, 2014 · 15 comments

Comments

@piranna
Copy link

piranna commented Jul 9, 2014

Allow to use EcmaScript 6 iterators and generators instead of Array objects.

@aearly
Copy link
Collaborator

aearly commented Jul 9, 2014

Unless I'm misunderstanding, isn't that what visionmedia/co is about, rather than async?

@piranna
Copy link
Author

piranna commented Jul 9, 2014

No, they are different things. Co is about flow control using generators, that's mostly an intelligent hack. What I'm talking about is to use generators in the same way that async.js use arrays for data processing in a similar way of Python itertools, where you can be able to use any iterable object (any object that implement the iterator protocol, like lists, dicts and generators).

@megawac
Copy link
Collaborator

megawac commented Jul 4, 2015

Support could be added pretty easily to keyIterator and iterator I think

@aearly
Copy link
Collaborator

aearly commented Jul 6, 2015

A quick workaround is async.map([...generator()], doSomething, done), but iterating over a lazy iterator would open up some cool control flow options.

@aearly
Copy link
Collaborator

aearly commented Mar 21, 2016

Closing this in favor of #839 -- I think supporting arbitrary ES6 iterators covers a lot of things -- Maps, Sets, Generators, etc...

@aearly aearly closed this as completed Mar 21, 2016
@piranna
Copy link
Author

piranna commented Mar 22, 2016

Yes, both issues are similar.

@aearly aearly reopened this Mar 24, 2016
@aearly
Copy link
Collaborator

aearly commented Mar 24, 2016

I just realized that generators don't implement the iterator protocol, meaning we'll need separate support for them. This will prove to be tricky, since there doesn't seem to be a bulletproof way to detect generator functions.

@megawac
Copy link
Collaborator

megawac commented Jun 30, 2016

@jdalton any interest in adding _.isGenerator =)

@megawac
Copy link
Collaborator

megawac commented Jun 30, 2016

Actually nevermind there's no reason for us to detect whether its a generator @aearly, if the iterable passed to keyIterator is a function we can just execute it and process the result as an iterator?

https://stackoverflow.com/a/19660350/1517919

@aearly
Copy link
Collaborator

aearly commented Jun 30, 2016

It's really tricky, since our iterator handling checks an object for a [Symbol.iterator] method, then calls .next() on the result of that method. For a generator, it would call the function itself, then call .next() on the result. I'm not sure if being "nextable" is sufficient enough.

@aearly
Copy link
Collaborator

aearly commented Jun 30, 2016

Perhaps internal/getIterator could check to see if the function's prototype matches a generator function's prototype. (Like in that stackoverflow question). I don't know how portable that strategy would be across JS engines, though. A robust _.isGenerator function would make this trivial to implement....

@megawac
Copy link
Collaborator

megawac commented Jun 30, 2016

I'd say if item is a function, we call the function assuming it will follow
generator protocol. Does that not suffice for our use case?

On Thu, Jun 30, 2016 at 5:22 PM, Alex Early [email protected]
wrote:

Perhaps internal/getIterator could check to see if the function's
prototype matches a generator function's prototype. (Like in that
stackoverflow question). I don't know how portable that strategy would be
across JS engines, though.


You are receiving this because you commented.
Reply to this email directly, view it on GitHub
#579 (comment), or mute
the thread
https://github.com/notifications/unsubscribe/ADUIECpwzLMYv9QPFq0SDaLF0RClBwJrks5qRDOEgaJpZM4CLfkD
.

@aearly
Copy link
Collaborator

aearly commented Jul 1, 2016

The only pitfall I can think of is when someone accidentally passes a function as the wrong arg -- you'd get an error like "undefined has no property next". Right now, I think it would try to iterate over the own properties of the function.

Can't think of any use case where passing a function would be a valid use case in the future, so it might be sufficient.

@131
Copy link

131 commented Aug 19, 2016

meanwhile, i am in need of async's signatures with async/await (through ES6 generator & co) supports. So, i ended up writing a dedicated project for it async-co.

@aearly
Copy link
Collaborator

aearly commented Apr 7, 2017

I think our current support is sufficient. You can do this today, and it works:

function* gen(count) {
  for (var i = 0; i < count; i++)
    yield i;
}

async.map(gen(5), (val, cb) => {
  cb(null, val * 2)
}, (err, res) => {
  // res = [0, 2, 4, 6, 8]
})

The generator support was within us the whole time. 👸 🌠 👠

@aearly aearly closed this as completed Apr 7, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants