While composing an async generator function, I noticed that the following construct results in a SyntaxError
:
async function * foo() {
await yield bar; // Can not use 'yield' as identifier inside a generator
}
Even though reversing the order of the contextual keywords is perfectly acceptable:
async function * foo() {
yield await bar; // OK
}
After reading the error carefully, I was able to correct the syntax by wrapping the UnaryExpression
within the AwaitExpression
in parentheses to avoid parsing the token yield
as an identifier instead of a contextual keyword:
async function * foo() {
await (yield bar); // OK
}
But this begs the question, what specific static semantics in ECMAScript 2018 are involved that cause yield
to be parsed as an identifier in this context, while await
does not require special treatment?