/Async-Await-Proposal-for-TypeScript

A specification for how async await should work in TypeScript.

Primary LanguageJavaScript

Async/Await Proposal for TypeScript

The TypeScript team is investigating Async/Await. What follows is a proposal for how Async/Await should be implemented. It embraces the widely accepted community standard for syntactically sane asynchronous programming: Promises/A+ spec. By using promises as the atomic unit for async/await in TypeScript, the language taps into a large, existing set of libraries (Q, WinJS, DOM Futures proposal, etc.), making them instantly compatible.

The need is clear - asynchronous behavior, and the handling of that behavior, can be difficult to reason about in JavaScript. TypeScript does little currently to address this problem. The Promises/A+ spec gets us most of the way there, making it possible to develop with asynchronous requests in a way that seems synchronous. There are still certain things the spec alone can not handle well - using promises directly in conditional statements, awaiting in loops, and assigning to a typed value using both an async function call and synchronously, depending on a branching statement.

Goals

  • Be able to handle all use cases covered by async/await in C#.
  • Output that can be easily grokked by un-initiated. This means having absolutely no mystery in the source transformations.
  • Ability to convert other asynchronous operations into promises.

Organization

  • Start with a simple syntax definition. I.e. this is how the code will look like when you write it.
  • Go through each situation where the await keyword introduces complexity in implementation from most basic to most advanced.
  • Each section will break down the syntax until it fits a pattern shown in a previous section or is valid current TypeScript.

An example from C#

See AsyncAwesome.cs. Notice there are a few different use cases that must be covered to consider our implementation a success:

  • await assignment can exist within a condition. In fact, the function can never await a task due to the branches taken, but still return the syncrhonously-assigned value wrapped in a task.
  • Tasks can be awaited in a loop synchronously. The next step will not be completed until the previous iteration completes.
  • awaits can be performed inside of conditions or as function parameters

Non-trivial Language Features to Preserve

  • Variable Hoisting
  • Binary Operators
  • Short-Circuiting Boolean Operators
  • Conditional Expressions
  • Loops
  • Switch
  • Try-catch-finally

Table of Contents

  1. Expressions
  2. Implicit Promise Interface
  3. Async Function Expression
  4. Defer Function Expression
  5. Await Expression
  6. Code Generation
  7. Async Function
  8. Defer Function
  9. Simple Await Expression
  10. Await Expressions in Compound Expressions
  11. Await in Boolean Expressions
  12. Await in Conditionals
  13. Await in Switch
  14. Await in Loops