15.8 Async Function Definitions
Syntax
await
is parsed as an
- In an
AsyncFunctionBody . - In the
FormalParameters of anAsyncFunctionDeclaration ,AsyncFunctionExpression ,AsyncGeneratorDeclaration , orAsyncGeneratorExpression .AwaitExpression in this position is a Syntax error viastatic semantics .
When await
is parsed as a await
may be parsed as an identifier when the [Await] parameter is absent. This includes the following contexts:
- Anywhere outside of an
AsyncFunctionBody orFormalParameters of anAsyncFunctionDeclaration ,AsyncFunctionExpression ,AsyncGeneratorDeclaration , orAsyncGeneratorExpression . - In the
BindingIdentifier of aFunctionExpression ,GeneratorExpression , orAsyncGeneratorExpression .
Unlike
15.8.1 Static Semantics: Early Errors
- It is a Syntax Error if
FunctionBodyContainsUseStrict ofAsyncFunctionBody istrue andIsSimpleParameterList ofUniqueFormalParameters isfalse . - It is a Syntax Error if
HasDirectSuper ofAsyncMethod istrue . - It is a Syntax Error if
UniqueFormalParameters Contains AwaitExpression istrue . - It is a Syntax Error if any element of the
BoundNames ofUniqueFormalParameters also occurs in theLexicallyDeclaredNames ofAsyncFunctionBody .
- It is a Syntax Error if
FunctionBodyContainsUseStrict ofAsyncFunctionBody istrue andIsSimpleParameterList ofFormalParameters isfalse . - It is a Syntax Error if
FormalParameters Contains AwaitExpression istrue . - If the source code matching
FormalParameters isstrict mode code , the Early Error rules for are applied.UniqueFormalParameters : FormalParameters - If
BindingIdentifier is present and the source code matchingBindingIdentifier isstrict mode code , it is a Syntax Error if theStringValue ofBindingIdentifier is"eval" or"arguments" . - It is a Syntax Error if any element of the
BoundNames ofFormalParameters also occurs in theLexicallyDeclaredNames ofAsyncFunctionBody . - It is a Syntax Error if
FormalParameters Contains SuperProperty istrue . - It is a Syntax Error if
AsyncFunctionBody Contains SuperProperty istrue . - It is a Syntax Error if
FormalParameters Contains SuperCall istrue . - It is a Syntax Error if
AsyncFunctionBody Contains SuperCall istrue .
15.8.2 Runtime Semantics: InstantiateAsyncFunctionObject
With parameter scope.
- Let name be
StringValue ofBindingIdentifier . - Let sourceText be the source text matched by
AsyncFunctionDeclaration . - Let F be !
OrdinaryFunctionCreate (%AsyncFunction.prototype% , sourceText,FormalParameters ,AsyncFunctionBody ,non-lexical-this , scope). - Perform !
SetFunctionName (F, name). - Return F.
- Let sourceText be the source text matched by
AsyncFunctionDeclaration . - Let F be !
OrdinaryFunctionCreate (%AsyncFunction.prototype% , sourceText,FormalParameters ,AsyncFunctionBody ,non-lexical-this , scope). - Perform !
SetFunctionName (F,"default" ). - Return F.
15.8.3 Runtime Semantics: InstantiateAsyncFunctionExpression
With optional parameter name.
- If name is not present, set name to
"" . - Let scope be the LexicalEnvironment of the
running execution context . - Let sourceText be the source text matched by
AsyncFunctionExpression . - Let closure be !
OrdinaryFunctionCreate (%AsyncFunction.prototype% , sourceText,FormalParameters ,AsyncFunctionBody ,non-lexical-this , scope). - Perform
SetFunctionName (closure, name). - Return closure.
Assert : name is not present.- Set name to
StringValue ofBindingIdentifier . - Let scope be the LexicalEnvironment of the
running execution context . - Let funcEnv be !
NewDeclarativeEnvironment (scope). - Perform ! funcEnv.CreateImmutableBinding(name,
false ). - Let sourceText be the source text matched by
AsyncFunctionExpression . - Let closure be !
OrdinaryFunctionCreate (%AsyncFunction.prototype% , sourceText,FormalParameters ,AsyncFunctionBody ,non-lexical-this , funcEnv). - Perform !
SetFunctionName (closure, name). - Perform ! funcEnv.InitializeBinding(name, closure).
- Return closure.
The
15.8.4 Runtime Semantics: EvaluateAsyncFunctionBody
With parameters functionObject and argumentsList (a
- Let promiseCapability be !
NewPromiseCapability (%Promise% ). - Let declResult be
FunctionDeclarationInstantiation (functionObject, argumentsList). - If declResult is not an
abrupt completion , then- Perform !
AsyncFunctionStart (promiseCapability,FunctionBody ).
- Perform !
- Else,
- Perform !
Call (promiseCapability.[[Reject]],undefined , « declResult.[[Value]] »).
- Perform !
- Return
Completion { [[Type]]:return , [[Value]]: promiseCapability.[[Promise]], [[Target]]:empty }.
15.8.5 Runtime Semantics: Evaluation
- Return
NormalCompletion (empty ).
- Return
NormalCompletion (empty ).
- Let exprRef be the result of evaluating
UnaryExpression . - Let value be ?
GetValue (exprRef). - Return ?
Await (value).