Experimental support for async functions

TypeScript 1.6 introduces experimental support of async functions when targeting ES6. Async functions are expected to invoke an asynchronous operation and await its result without blocking normal execution of the program. This accomplished through the use of an ES6-compatible Promise implementation, and transposition of the function body into a compatible form to resume execution when the awaited asynchronous operation completes.

An async function is a function or method that has been prefixed with the async modifier. This modifier informs the compiler that function body transposition is required, and that the keyword await should be treated as a unary expression instead of an identifier. An Async Function must provide a return type annotation that points to a compatible Promise type. Return type inference can only be used if there is a globally defined, compatible Promise type.

Example
  1. var p: Promise<number> = /* ... */;
  2. async function fn(): Promise<number> {
  3. var i = await p; // suspend execution until 'p' is settled. 'i' has type "number"
  4. return 1 + i;
  5. }
  6. var a = async (): Promise<number> => 1 + await p; // suspends execution.
  7. var a = async () => 1 + await p; // suspends execution. return type is inferred as "Promise<number>" when compiling with --target ES6
  8. var fe = async function(): Promise<number> {
  9. var i = await p; // suspend execution until 'p' is settled. 'i' has type "number"
  10. return 1 + i;
  11. }
  12. class C {
  13. async m(): Promise<number> {
  14. var i = await p; // suspend execution until 'p' is settled. 'i' has type "number"
  15. return 1 + i;
  16. }
  17. async get p(): Promise<number> {
  18. var i = await p; // suspend execution until 'p' is settled. 'i' has type "number"
  19. return 1 + i;
  20. }
  21. }