Stage 0 Draft / January 20, 2021

Async do expressions

Introduction

async do expressions allow you to introduce an asynchronous context within synchronous code without needing an immediately-invoked async function expression.

1 Async Do Expressions

Syntax

AsyncDoExpression : async [no LineTerminator here] do Block[~Yield, +Await, ~Return] Note

Unlike do expressions, yield can never be used within an async do expression.

1.1 Static Semantics: Early Errors

AsyncDoExpression : async do Block

TODO: pretty much the same list as for regular do expressions.

1.2 Runtime Semantics: Evaluation

AsyncDoExpression : async do Block
  1. Let promiseCapability be ! NewPromiseCapability(%Promise%).
  2. Perform ! AsyncDoStart(promiseCapability, Block).
  3. Return Completion { [[Type]]: return, [[Value]]: promiseCapability.[[Promise]], [[Target]]: empty }.

1.2.1 AsyncDoStart ( promiseCapability, block )

The abstract operation AsyncDoStart takes arguments promiseCapability (a PromiseCapability Record) and block (a Parse Node for a Block). It performs the following steps when called:

  1. NOTE: the algorithm below is identical to AsyncFunctionStart(promiseCapability, Block) except for its handling of the final completion value.
  2. Let runningContext be the running execution context.
  3. Let asyncContext be a copy of runningContext.
  4. NOTE: Copying the execution state is required for the step below to resume its execution. It is ill-defined to resume a currently executing context.
  5. Set the code evaluation state of asyncContext such that when evaluation is resumed for that execution context the following steps will be performed:
    1. Let result be the result of evaluating block.
    2. Assert: If we return here, the block either threw an exception or reached the end of its body; all awaiting is done.
    3. Remove asyncContext from the execution context stack and restore the execution context that is at the top of the execution context stack as the running execution context.
    4. If result.[[Type]] is normal, then
      1. Let result be UpdateEmpty(result, undefined).
      2. Perform ! Call(promiseCapability.[[Resolve]], undefined, « result.[[Value]] »).
    5. Else,
      1. Assert: result.[[Type]] is throw.
      2. Perform ! Call(promiseCapability.[[Reject]], undefined, « result.[[Value]] »).
    6. Return.
  6. Push asyncContext onto the execution context stack; asyncContext is now the running execution context.
  7. Resume the suspended evaluation of asyncContext. Let result be the value returned by the resumed computation.
  8. Assert: When we return here, asyncContext has already been removed from the execution context stack and runningContext is the currently running execution context.
  9. Assert: result is a normal completion with a value of undefined. The possible sources of completion values are Await or, if the async do doesn't await anything, step 5.f above.
  10. Return.

2 Integration

Syntax

PrimaryExpression[Yield, Await] : AsyncDoExpression Note

Unlike do expressions, async do expressions can be used in statement position.

A Copyright & Software License

Copyright Notice

© 2021 Kevin Gibbons

Software License

All Software contained in this document ("Software") is protected by copyright and is being made available under the "BSD License", included below. This Software may be subject to third party rights (rights from parties other than Ecma International), including patent rights, and no licenses under such third party rights are granted under this license even if the third party concerned is a member of Ecma International. SEE THE ECMA CODE OF CONDUCT IN PATENT MATTERS AVAILABLE AT https://ecma-international.org/memento/codeofconduct.htm FOR INFORMATION REGARDING THE LICENSING OF PATENT CLAIMS THAT ARE REQUIRED TO IMPLEMENT ECMA INTERNATIONAL STANDARDS.

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:

  1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
  2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
  3. Neither the name of the authors nor Ecma International may be used to endorse or promote products derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE ECMA INTERNATIONAL "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL ECMA INTERNATIONAL BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.