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

Support async generators #682

Merged
merged 1 commit into from
Jul 7, 2019
Merged

Support async generators #682

merged 1 commit into from
Jul 7, 2019

Conversation

dougmoscrop
Copy link
Contributor

Not sure if there's interest in landing this - I guess it's a question of whether you want to drop IE11 support or introduce babel or something, so there's two commits - one just uses async/await, but the grunt task fails. The other (most recent) uses Promise.resolve and the grunt task works.

.eslintrc Outdated Show resolved Hide resolved
@dougmoscrop
Copy link
Contributor Author

I'll add tests once we confirm that this is wanted

@vqvu
Copy link
Collaborator

vqvu commented Jun 6, 2019 via email

@dougmoscrop
Copy link
Contributor Author

Yes, no problem, thanks.

@dougmoscrop
Copy link
Contributor Author

dougmoscrop commented Jun 13, 2019

Actually just to be clear, is the desired interface such that you do something like _.fromAsyncGenerator(...)?

It's not clear to me how I can detect within _(thing) that thing is an async generator that I should resolve the promises on, I think maybe I can detect if the result from next() is thenable, I'll test around but it depends on whether or not you want this to be supported from _() directly or only with a helper

@vqvu
Copy link
Collaborator

vqvu commented Jun 13, 2019 via email

@dougmoscrop
Copy link
Contributor Author

dougmoscrop commented Jul 3, 2019

Sorry it took so long.

I don't intend to be argumentative, but you mentioned that it's currently resolving promises - I don't believe it is, in fact I think I wrote test that shows this. The only promise that gets resolved is the one that comes from .next() (if it returns one -- generators don't, async generators do), not iterElem.value -- it is left unresolved (if userland send an unresolved promise to the stream)

I only did this since I personally prefer to keep the interface as simple as possible (_(most_things)) and consistent (no _.fromGenerator exists)

The current impl is a bit ugly because I got rid of the "hoisting" of everything in to a Promise(), and instead I have to detect if the result of it.next() is 'thenable' (promise would do this for us)

Let me know what you think?

Copy link
Collaborator

@vqvu vqvu left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you also update update the documentation at the top of index.js to add an entry for Asynchronous Iterator? Link to https://github.com/tc39/proposal-async-iteration.

lib/index.js Outdated
.then(pushIt)
.catch(function(err) {
// this is needed, but not sure why
_.setImmediate(function() {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this still necessary? I tried pulling your changes and ran the test without setImmediate, and they all passed.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, sorry that comment is poorly phrased: I have "real" code that misbehaves when this is not present, but the tests are fine; I hope to add a test that demonstrates the problem, I just have to come up with one.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A red herring I think, it was because of tomfoolery in some other part of code (I was using observe().forEach() without handling errors and that cause some.. oddness).

I've removed this.

@vqvu
Copy link
Collaborator

vqvu commented Jul 4, 2019

You're absolutely right. I was confused about what the result of calls to next() would be. I incorrectly assumed that async generators return objects of type {value: Promise<T>, done: boolean}, but they actually return objects of type Promise<{value: T, done: boolean}>.

I have no more major concerns.

@dougmoscrop
Copy link
Contributor Author

OK, I took a stab at the docs and removed the _.setImmediate weirdness. Let me know if there's anything else!

@vqvu
Copy link
Collaborator

vqvu commented Jul 7, 2019

LGTM. Thanks for the contribution!

@vqvu vqvu merged commit 97397c1 into caolan:master Jul 7, 2019
vqvu added a commit that referenced this pull request Jul 7, 2019
This is now possible thanks to the work in #682.
vqvu added a commit that referenced this pull request Jul 7, 2019
This is now possible thanks to the work in #682.
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.

2 participants