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
Implement support for async generator functions and for-await statements #3473
Conversation
(function () { | ||
_this2; | ||
}); | ||
() => { |
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.
I looks to me like the current async-to-generator transform is unnecessarily transforming arrows that are nested within nested function declarations and expressions.
Is there some reason why arrows nested within a nested function would need to be translated?
45d9c7a
to
ef1a907
Compare
Since this functionality was in Babel v5, any idea when this will make it into the current Babel? |
Made the babylon release https://github.com/babel/babylon/releases/tag/v6.8.0 |
ef1a907
to
f19c10d
Compare
Current coverage is 87.84%
@@ master #3473 diff @@
==========================================
Files 194 197 +3
Lines 10119 10173 +54
Methods 1535 1544 +9
Messages 0 0
Branches 2245 2253 +8
==========================================
+ Hits 8886 8936 +50
- Misses 1233 1237 +4
Partials 0 0
|
Can someone help explain the failing "codecov/changes" check? I can quite figure out what's going on there. |
I think there are some issues with codecov (recently), I wouldn't worry about it atm |
OK. It seems like we should have some eval tests, but I'm not sure if that's really possible given the async nature of this transform? Are there any functional tests for async functions? |
I don't think there have been - only https://github.com/babel/babel/blob/master/packages/babel-plugin-transform-async-to-generator/test/fixtures/regression/T6882/exec.js which was a test for hoisting http://phabricator.babeljs.io/T6882. Otherwise I would check out the existing exec.js tests |
Anymore progress on this? This unlocks a lot of potential and code broke since it was in Babel v5 :( |
Then, finally we can iterate nicely over a database query cursor |
Is there anything I can help with here to get this integrated into a Babel release? |
@jprichardson There a failing code coverage test which I can't make heads or tails of, and there really ought to be some sort of "exec" tests, but I haven't had time to sit down and figure out how to make that work for async operations. |
@zenparsing Don't worry about the coverage issue like I mentioned above |
Just found out that Babel does NOT transpile |
@wifiextender Correct, you load |
According to |
@tonyxiao that is if you use the regenerator runtime. This seems to be for use with native generators. |
I see. @Kovensky got it thanks. |
@zenparsing, do you still have plans to finish this? It will be awesome! |
Isn't this now possible with this https://github.com/babel/babel/tree/master/packages/babel-plugin-syntax-async-generators ? |
Yes but we need a code review and some more exec tests as mentioned above - unless we want users to be our testers 😛 |
FYI, we're hoping to push the async iteration proposal to stage 3 at the next TC39 meeting (in two weeks' time). I totally understand if the Babel maintainers don't have the time to take this pull request over the finish line, but it would be ideal if Babel supported such a stage 3 feature. |
I'l try to take a look this week or at least finish up the tests so we can get this used/tested |
@zenparsing can you fixup the merge conflicts? If I don't get around to the tests we'll just merge as is so we can get something out there like @domenic said |
Going to merge manually in another pr |
Made #4576, feel free to comment there. (fixed merge conflicts, added async exec test) |
Thanks @zenparsing, sorry for the super long delay |
This PR depends on babel/babylon@b926e40. Tests will fail until the next babylon release, but submitting early for feedback.
This change implements the async iteration proposal, currently at stage 2. It includes the following features:
async function* g() { }
) to wrapped generator functions, similar to the current async-to-generator transform.for-await
statements into for loops containing yield expressions.Thanks!
Implementation Notes
Additional comments inline.
Async Generator Functions
Async generator functions are transformed into regular generator functions, wrapped with a call to a new helper function (
asyncGenerator.wrap
). Within the async generator function, await expressions are translated into yield expressions, where the argument is "boxed" using another helper function (asyncGenerator.await
). The wrapper function detects instances of the "boxed" values and knows that it should "pump" the generator with the awaited value instead of yielding to the consumer.Rather than duplicate the async wrapping transform in
babel-helper-remap-async-to-generator
, I chose to refactor that helper to allow an optional await wrapping callee.for-await
Since
for-await
statements can also appear inside of regular async functions and must take into account whether or not await expressions are "boxed", thefor-await
translation is done insidebabel-helper-remap-async-to-generator
. Most of thefor-await
transform logic comes from the existingfor-of
transformer.