15.8 Async Function Definitions

Syntax

AsyncFunctionDeclaration[Yield, Await, Default] : async [no LineTerminator here] function BindingIdentifier[?Yield, ?Await] ( FormalParameters[~Yield, +Await] ) { AsyncFunctionBody } [+Default] async [no LineTerminator here] function ( FormalParameters[~Yield, +Await] ) { AsyncFunctionBody } AsyncFunctionExpression : async [no LineTerminator here] function BindingIdentifier[~Yield, +Await]opt ( FormalParameters[~Yield, +Await] ) { AsyncFunctionBody } AsyncMethod[Yield, Await] : async [no LineTerminator here] PropertyName[?Yield, ?Await] ( UniqueFormalParameters[~Yield, +Await] ) { AsyncFunctionBody } AsyncFunctionBody : FunctionBody[~Yield, +Await] AwaitExpression[Yield] : await UnaryExpression[?Yield, +Await] Note 1

await is parsed as an AwaitExpression when the [Await] parameter is present. The [Await] parameter is present in the following contexts:

When Module is the syntactic goal symbol and the [Await] parameter is absent, await is parsed as a keyword and will be a Syntax error. When Script is the syntactic goal symbol, await may be parsed as an identifier when the [Await] parameter is absent. This includes the following contexts:

Note 2

Unlike YieldExpression, it is a Syntax Error to omit the operand of an AwaitExpression. You must await something.

15.8.1 Static Semantics: Early Errors

AsyncMethod : async PropertyName ( UniqueFormalParameters ) { AsyncFunctionBody } AsyncFunctionDeclaration : async function BindingIdentifier ( FormalParameters ) { AsyncFunctionBody } async function ( FormalParameters ) { AsyncFunctionBody } AsyncFunctionExpression : async function BindingIdentifieropt ( FormalParameters ) { AsyncFunctionBody }

15.8.2 Runtime Semantics: InstantiateAsyncFunctionObject

With parameter scope.

AsyncFunctionDeclaration : async function BindingIdentifier ( FormalParameters ) { AsyncFunctionBody }
  1. Let name be StringValue of BindingIdentifier.
  2. Let sourceText be the source text matched by AsyncFunctionDeclaration.
  3. Let F be ! OrdinaryFunctionCreate(%AsyncFunction.prototype%, sourceText, FormalParameters, AsyncFunctionBody, non-lexical-this, scope).
  4. Perform ! SetFunctionName(F, name).
  5. Return F.
AsyncFunctionDeclaration : async function ( FormalParameters ) { AsyncFunctionBody }
  1. Let sourceText be the source text matched by AsyncFunctionDeclaration.
  2. Let F be ! OrdinaryFunctionCreate(%AsyncFunction.prototype%, sourceText, FormalParameters, AsyncFunctionBody, non-lexical-this, scope).
  3. Perform ! SetFunctionName(F, "default").
  4. Return F.

15.8.3 Runtime Semantics: InstantiateAsyncFunctionExpression

With optional parameter name.

AsyncFunctionExpression : async function ( FormalParameters ) { AsyncFunctionBody }
  1. If name is not present, set name to "".
  2. Let scope be the LexicalEnvironment of the running execution context.
  3. Let sourceText be the source text matched by AsyncFunctionExpression.
  4. Let closure be ! OrdinaryFunctionCreate(%AsyncFunction.prototype%, sourceText, FormalParameters, AsyncFunctionBody, non-lexical-this, scope).
  5. Perform SetFunctionName(closure, name).
  6. Return closure.
AsyncFunctionExpression : async function BindingIdentifier ( FormalParameters ) { AsyncFunctionBody }
  1. Assert: name is not present.
  2. Set name to StringValue of BindingIdentifier.
  3. Let scope be the LexicalEnvironment of the running execution context.
  4. Let funcEnv be ! NewDeclarativeEnvironment(scope).
  5. Perform ! funcEnv.CreateImmutableBinding(name, false).
  6. Let sourceText be the source text matched by AsyncFunctionExpression.
  7. Let closure be ! OrdinaryFunctionCreate(%AsyncFunction.prototype%, sourceText, FormalParameters, AsyncFunctionBody, non-lexical-this, funcEnv).
  8. Perform ! SetFunctionName(closure, name).
  9. Perform ! funcEnv.InitializeBinding(name, closure).
  10. Return closure.
Note

The BindingIdentifier in an AsyncFunctionExpression can be referenced from inside the AsyncFunctionExpression's AsyncFunctionBody to allow the function to call itself recursively. However, unlike in a FunctionDeclaration, the BindingIdentifier in a AsyncFunctionExpression cannot be referenced from and does not affect the scope enclosing the AsyncFunctionExpression.

15.8.4 Runtime Semantics: EvaluateAsyncFunctionBody

With parameters functionObject and argumentsList (a List).

AsyncFunctionBody : FunctionBody
  1. Let promiseCapability be ! NewPromiseCapability(%Promise%).
  2. Let declResult be FunctionDeclarationInstantiation(functionObject, argumentsList).
  3. If declResult is not an abrupt completion, then
    1. Perform ! AsyncFunctionStart(promiseCapability, FunctionBody).
  4. Else,
    1. Perform ! Call(promiseCapability.[[Reject]], undefined, « declResult.[[Value]] »).
  5. Return Completion { [[Type]]: return, [[Value]]: promiseCapability.[[Promise]], [[Target]]: empty }.

15.8.5 Runtime Semantics: Evaluation

AsyncFunctionDeclaration : async function BindingIdentifier ( FormalParameters ) { AsyncFunctionBody }
  1. Return NormalCompletion(empty).
AsyncFunctionDeclaration : async function ( FormalParameters ) { AsyncFunctionBody }
  1. Return NormalCompletion(empty).
AsyncFunctionExpression : async function BindingIdentifieropt ( FormalParameters ) { AsyncFunctionBody }
  1. Return InstantiateAsyncFunctionExpression of AsyncFunctionExpression.
AwaitExpression : await UnaryExpression
  1. Let exprRef be the result of evaluating UnaryExpression.
  2. Let value be ? GetValue(exprRef).
  3. Return ? Await(value).