Skip to content
This repository has been archived by the owner on Sep 8, 2021. It is now read-only.

Why not allow await? #24

Closed
bakkot opened this issue Nov 8, 2020 · 6 comments
Closed

Why not allow await? #24

bakkot opened this issue Nov 8, 2020 · 6 comments
Labels
duplicate This issue or pull request already exists

Comments

@bakkot
Copy link

bakkot commented Nov 8, 2020

It's surprising to me that this forbids await. I'd expect to be able to do anything I could do in the surrounding code, including initialization of the class itself which might require waiting for some asynchronous operation.

Note that you can already await in computed property names:

(async () => class {
  [await 0](){}
})()

is legal today.

This can't just be legalized in a followup because it interacts with parsing: if the await flag is not passed to the static block, then

class A {
  static {
    console.log(await);
  }
}

is syntactically legal even in an async context. To keep the possibility of legalizing this open, at the very least we'd need to pass the correct grammar flag today and just make AwaitExpression an error. But it seems both simpler and more useful to inherit async-ness from the surrounding context.

(Edit: I should mention that similar concerns apply to yield, though the parsing isn't as tricky.)

@ljharb
Copy link
Member

ljharb commented Nov 9, 2020

Wouldn't allowing it mean that the partially-constructed class would be observable for more than just the rest of the static block?

@bakkot
Copy link
Author

bakkot commented Nov 9, 2020

How so? Code after the class wouldn't run until the await completed, and code in other functions wouldn't have visibility of the class.

@ljharb
Copy link
Member

ljharb commented Nov 9, 2020

What I mean is, the class could provide its partially-constructed class to other code, and then finish with the static block code after N ticks had passed and the class had been mutated. I'm not sure why that seems worse to me than the exact same thing happening synchronously, though, so maybe it's fine.

@rbuckton
Copy link
Collaborator

This is essentially a duplicate of #7 and you can read my rationale for why this is currently disallowed there. We may need to do something more to disallow await as an IdentifierReference inside of a static {} block to ensure we future-proof it should we decide to allow it later.

There shouldn't be a concern for yield, as yield is treated as a reserved word in strict mode so its already disallowed as an identifier inside of a class body. Its unfortunate await isn't treated as a reserved word in strict mode as well.

@rbuckton
Copy link
Collaborator

I'm closing this as a duplicate of #7. I've opened #27 to discuss the restrictions needed to both disallow await but leave it as an option for a follow-on proposal.

@bakkot
Copy link
Author

bakkot commented Nov 10, 2020

#7 doesn't really include any rationale, that I can see. It just says it's not permitted.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
duplicate This issue or pull request already exists
Projects
None yet
Development

No branches or pull requests

3 participants