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

transform-async-generator-functions not transform for-await in async functions #4969

Closed
hax opened this issue Dec 8, 2016 · 8 comments
Closed
Assignees
Labels
Has PR i: bug outdated A closed issue/PR that is archived due to age. Recommended to make a new issue

Comments

@hax
Copy link

hax commented Dec 8, 2016

Input Code

async function foo() {
	for await (const x of y) {}
}
async function* bar() {
	for await (const x of y) {}
}

Babel Configuration (.babelrc, package.json, cli command)

{
	"plugins": [
		"transform-async-generator-functions"
	],
}

Expected Behavior

Both bar() and for-await-of in foo() should be transformed.

Current Behavior

for-await-of in foo() is not transformed.

Possible Solution

As I understand, for await (const x of y) { x } is like a sugar of: for (const x of y) { await x } (but call y[Symbol.asyncIterator]() instead of y[Symbol.iterator]())
Maybe we can introduce a separate plugin transform-for-await-of.

@xtuc xtuc added the i: bug label Dec 15, 2016
@Jamesernator
Copy link
Contributor

It looks like the logic for converting for-await loops is actually in remap-async-to-generator for some reason, which is a pain because it means that in order to use for-await in an async function you have to also convert them into generators.

I might have a look this week into moving it into its own helper and make async-generator-functions use it.

@Jessidhia
Copy link
Member

Jessidhia commented Feb 21, 2017

What's actually a Promise, in async iterators, is the the iterator result object itself, which is why new syntax, symbols, etc were required.

for await is actually closer to a sugar of:

// _very_ incomplete sugar
for (
  let iter = y[Symbol.asyncIterator](), step = await iter.next();
  !step.done;
  step = await iter.next()
) {
  const x = step.value // note, no await here!
}

@iamchenxin
Copy link

@Jamesernator Does "transform-async-generator-functions" work well now? I test with '6.24.1', still do not work. And it seems it will transform 'for await' to 'generators' with an additional plugin 'transform-async-to-generator'.

@alexeyraspopov
Copy link

#7446 is merged. Should this issue be closed?

@existentialism
Copy link
Member

@alexeyraspopov thanks!

@alexeyraspopov
Copy link

@existentialism, not sure about proper place to ask, but do you know what else should happen so the babel-preset-env will start supporting async generators (and object rest/spread I guess)?

@mnpenner
Copy link

How do I 'get' this fix?

My .babelrc:

{
    "plugins": [
        "transform-object-rest-spread",
        "transform-class-properties",
        "transform-function-bind",
        "syntax-async-generators",
        "transform-async-generator-functions"
    ],
    "presets": [
        [
            "env",
            {
                "targets": {
                    "node": "current"
                }
            }
        ]
    ],
    "compact": false
}

And package.json:

  "devDependencies": {
    "babel-cli": "^6.26.0",
    "babel-plugin-syntax-async-generators": "^6.13.0",
    "babel-plugin-transform-async-generator-functions": "^6.24.1",
    "babel-plugin-transform-class-properties": "^6.24.1",
    "babel-plugin-transform-function-bind": "^6.22.0",
    "babel-plugin-transform-object-rest-spread": "^6.26.0",
    "babel-preset-env": "^1.6.1"
  }

What versions do I have to bump? The fix is on npm, right?

@nicolo-ribaudo
Copy link
Member

It's in Babel 7 beta.

@lock lock bot added the outdated A closed issue/PR that is archived due to age. Recommended to make a new issue label Jun 27, 2018
@lock lock bot locked as resolved and limited conversation to collaborators Jun 27, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Has PR i: bug outdated A closed issue/PR that is archived due to age. Recommended to make a new issue
Projects
None yet
Development

No branches or pull requests

9 participants