15.8 Async Function Definitions
Syntax
await
is parsed as a
- In an
AsyncFunctionBody . - In the
FormalParameters of anAsyncFunctionDeclaration ,AsyncFunctionExpression ,AsyncGeneratorDeclaration , orAsyncGeneratorExpression .AwaitExpression in this position is a Syntax error viastatic semantics . - In a
Module .
When 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 text matched by FormalParameters isstrict mode code , the Early Error rules for are applied.UniqueFormalParameters : FormalParameters - If
BindingIdentifier is present and thesource text matched by BindingIdentifier isstrict mode code , it is a Syntax Error if theStringValue ofBindingIdentifier is either"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
The
- 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 , env, privateEnv). - 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 , env, privateEnv). - Perform
SetFunctionName (F,"default" ). - Return F.
15.8.3 Runtime Semantics: InstantiateAsyncFunctionExpression
The
- If name is not present, set name to
"" . - Let env be the LexicalEnvironment of the
running execution context . - Let privateEnv be the
running execution context 's PrivateEnvironment. - Let sourceText be the
source text matched by AsyncFunctionExpression . - Let closure be
OrdinaryFunctionCreate (%AsyncFunction.prototype% , sourceText,FormalParameters ,AsyncFunctionBody ,non-lexical-this , env, privateEnv). - Perform
SetFunctionName (closure, name). - Return closure.
Assert : name is not present.- Set name to
StringValue ofBindingIdentifier . - Let outerEnv be the LexicalEnvironment of the
running execution context . - Let funcEnv be
NewDeclarativeEnvironment (outerEnv). - Perform ! funcEnv.CreateImmutableBinding(name,
false ). - Let privateEnv be the
running execution context 's PrivateEnvironment. - Let sourceText be the
source text matched by AsyncFunctionExpression . - Let closure be
OrdinaryFunctionCreate (%AsyncFunction.prototype% , sourceText,FormalParameters ,AsyncFunctionBody ,non-lexical-this , funcEnv, privateEnv). - Perform
SetFunctionName (closure, name). - Perform ! funcEnv.InitializeBinding(name, closure).
- Return closure.
The
15.8.4 Runtime Semantics: EvaluateAsyncFunctionBody
The
- Let promiseCapability be !
NewPromiseCapability (%Promise% ). - Let declResult be
Completion (FunctionDeclarationInstantiation (functionObject, argumentsList)). - If declResult is an
abrupt completion , then- Perform !
Call (promiseCapability.[[Reject]],undefined , « declResult.[[Value]] »).
- Perform !
- Else,
- Perform
AsyncFunctionStart (promiseCapability,FunctionBody ).
- Perform
- Return
Completion Record { [[Type]]:return , [[Value]]: promiseCapability.[[Promise]], [[Target]]:empty }.
15.8.5 Runtime Semantics: Evaluation
- Let exprRef be ?
Evaluation ofUnaryExpression . - Let value be ?
GetValue (exprRef). - Return ?
Await (value).