{"action":"created","issue":{"url":"https://api.github.com/repos/Microsoft/TypeScript/issues/1664","labels_url":"https://api.github.com/repos/Microsoft/TypeScript/issues/1664/labels{/name}","comments_url":"https://api.github.com/repos/Microsoft/TypeScript/issues/1664/comments","events_url":"https://api.github.com/repos/Microsoft/TypeScript/issues/1664/events","html_url":"https://github.com/Microsoft/TypeScript/issues/1664","id":54276542,"number":1664,"title":"Proposal: Async Functions","user":{"login":"rbuckton","id":3902892,"avatar_url":"https://avatars.githubusercontent.com/u/3902892?v=3","gravatar_id":"","url":"https://api.github.com/users/rbuckton","html_url":"https://github.com/rbuckton","followers_url":"https://api.github.com/users/rbuckton/followers","following_url":"https://api.github.com/users/rbuckton/following{/other_user}","gists_url":"https://api.github.com/users/rbuckton/gists{/gist_id}","starred_url":"https://api.github.com/users/rbuckton/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/rbuckton/subscriptions","organizations_url":"https://api.github.com/users/rbuckton/orgs","repos_url":"https://api.github.com/users/rbuckton/repos","events_url":"https://api.github.com/users/rbuckton/events{/privacy}","received_events_url":"https://api.github.com/users/rbuckton/received_events","type":"User","site_admin":false},"labels":[{"url":"https://api.github.com/repos/Microsoft/TypeScript/labels/Spec","name":"Spec","color":"207de5"},{"url":"https://api.github.com/repos/Microsoft/TypeScript/labels/Suggestion","name":"Suggestion","color":"006b75"}],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":144,"created_at":"2015-01-14T02:25:40Z","updated_at":"2015-10-07T18:54:31Z","closed_at":null,"body":"# Async Functions\r\n\r\n# <a name=\"1\"/>1 Async Functions\r\n\r\nThis is a spec proposal for the addition of *Async Functions* (also known as `async..await`) as a feature of TypeScript.\r\n\r\n# <a name=\"2\"/>2 Use Cases\r\n\r\n*Async Functions* allow TypeScript developers to author functions that 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.\r\n\r\nThis is based primarily on the Async Functions strawman proposal for ECMAScript, and C# 5.0 § 10.15 *Async Functions*.\r\n\r\n# <a name=\"3\"/>3 Introduction\r\n\r\n## <a name=\"3.1\"/>3.1 Syntax\r\n\r\nAn *Async Function* is a *JavaScript Function*, *Parameterized Arrow Function*, *Method*, or *Get Accessor* 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.\r\n\r\nExample:\r\n\r\n```TypeScript\r\nvar p: Promise<number> = /* ... */;  \r\nasync function fn(): Promise<number> {  \r\n  var i = await p; // suspend execution until 'p' is settled. 'i' has type \"number\"  \r\n  return 1 + i;  \r\n}  \r\n  \r\nvar a = async (): Promise<number> => 1 + await p; // suspends execution.  \r\nvar a = async () => 1 + await p; // suspends execution. return type is inferred as \"Promise<number>\" when compiling with --target ES6  \r\nvar fe = async function(): Promise<number> {  \r\n  var i = await p; // suspend execution until 'p' is settled. 'i' has type \"number\"  \r\n  return 1 + i;  \r\n}  \r\n  \r\nclass C {  \r\n  async m(): Promise<number> {  \r\n    var i = await p; // suspend execution until 'p' is settled. 'i' has type \"number\"  \r\n    return 1 + i;  \r\n  }  \r\n  \r\n  async get p(): Promise<number> {  \r\n    var i = await p; // suspend execution until 'p' is settled. 'i' has type \"number\"  \r\n    return 1 + i;  \r\n  }  \r\n}\r\n```\r\n\r\n## <a name=\"3.2\"/>3.2 Transformations\r\n\r\nTo support this feature, the compiler needs to make certain transformations to the function body of an *Async Function*. The type of transformations performed depends on whether the current compilation target is ES6, or ES5/ES3.\r\n\r\n### <a name=\"3.2.1\"/>3.2.1 ES6 Transformations\r\n\r\nDuring compilation of an *Async Function* when targeting ES6, the following transformations are applied:\r\n\r\n* The new function body consists of a single return statement whose expression is a new instance of the promise type supplied as the return type of the *Async Function*.\r\n* The original function body is enclosed in a *Generator Function*.\r\n* Any `await` expressions inside the original function body are transformed into a compatible `yield` expression.\r\n  * As the precedence of `yield` is much lower than `await`, it may be necessary to enclose the `yield` expression in parenthesis if it is contained in the left-hand-side of a binary expression.\r\n* The *Generator Function* is executed and the resulting generator is passed as an argument to the `__awaiter` helper function.\r\n* The result of the `__awaiter` helper function is passed as an argument to the promise resolve callback.\r\n\r\nExample:\r\n\r\n```TypeScript\r\n// async.ts  \r\nvar p0: Promise<number> = /* ... */;  \r\nvar p1: Promise<number> = /* ... */;  \r\nasync function fn() {  \r\n  var i = await p0;  \r\n  return await p1 + i;  \r\n}  \r\n  \r\n// async.js  \r\nvar __awaiter = /* ... */;  \r\n  \r\nfunction fn() {  \r\n  return new Promise(function (_resolve) {  \r\n    _resolve(__awaiter(function* () {  \r\n      var i = yield p0;  \r\n      return (yield p1) + i;  \r\n    }()));  \r\n  });  \r\n}\r\n```\r\n\r\n### <a name=\"3.2.2\"/>3.2.2 ES5/ES3 Transformations\r\n\r\nAs ES5 and earlier do not support *Generator Functions*, a more complex transformation is required. To support this transformation, `__generator` helper function will be also emitted along with the `__awaiter` helper function, and a much more comprehensive set of transformations would be applied:\r\n\r\n* The new function body consists of a single return statement whose expression is a new instance of the promise type supplied as the return type of the *Async Function*.\r\n* The original function body is enclosed in a function expression with a single parameter that is passed as an argument to the `__generator` helper function.\r\n* All hoisted declarations (variable declarations and function declarations) in the original function body are extracted and added to the top of the new function body.\r\n* The original function body is rewritten into a series of case clauses in a switch statement.\r\n* Each statement in the function body that contains an `await` expression is rewritten into flattened set of instructions that are interpreted by the `__generator` helper function.\r\n* Temporary locals are generated to hold onto the values for partially-applied expressions to preserve side effects expected in the original source.\r\n* Logical binary expressions that contain an `await` expression are rewritten to preserve shortcutting.\r\n* Assignment expressions that contain an `await` are rewritten to store portions left-hand side of the assignment in temporary locals to preserve side effects.\r\n* Call expressions that contain an `await` in the argument list are rewritten to store the callee and this argument, and to instead call the `call` method of the callee.\r\n* New expressions that contain an `await` in the argument list are rewritten to preserve side effects.\r\n* Array literal expressions that contain an `await` in the element list are rewritten to preserve side effects.\r\n* Object literal expressions that contain an `await` in the element list are rewritten to preserve side effects.\r\n* try  statements that contain an  await  in the  try  block,  catch  clause, or  finally  block are rewritten and tracked as a protected region by the `__generator` helper function.\r\n* The variable of a `catch` clause is renamed to a unique identifier and all instances of the symbol are renamed to preserve the block scope behavior of a catch variable.\r\n* `break` and `continue` statements whose target is a statement that contains an `await` expression are rewritten to return an instruction interpreted by the `__generator` helper function.\r\n* `return` statements are rewritten to return an instruction interpreted by the `__generator` helper function.\r\n* `for..in` statements that contain an `await` expression are rewritten to capture the enumerable keys of the expression into a temporary array, to allow the iteration to be resumed when control is returned to the function following an `await` expression.\r\n* Labeled statements that contain an `await` expression are rewritten.\r\n* `await` expressions are rewritten to return an instruction interpreted by the  `__generator` helper function.\r\n\r\nExample:\r\n\r\n```TypeScript\r\n// async.ts  \r\nvar p0: Promise<number> = /* ... */;  \r\nvar p1: Promise<number> = /* ... */;  \r\nasync function fn() {  \r\n    var i = await p0;  \r\n    return await p1 + i;  \r\n}  \r\n  \r\n// async.js  \r\nvar __awaiter = /* ... */;  \r\nvar __generator = /* ... */;  \r\n  \r\nfunction fn() {  \r\n    var i;  \r\n    return new Promise(function (_resolve) {  \r\n        resolve(__awaiter(__generator(function (_state) {  \r\n            switch (_state.label) {  \r\n                case 0: return [3 /*yield*/, p0];  \r\n                case 1:  \r\n                    i = _state.sent;  \r\n                    return [3 /*yield*/, p1];  \r\n                case 2:  \r\n                    return [2 /*return*/, _state.sent + i];  \r\n            }  \r\n        }));  \r\n    });  \r\n}\r\n```\r\n\r\nThe following is an example of an async function that contains a `try` statement:\r\n\r\n```TypeScript\r\n// async.ts  \r\nvar p0: Promise<number> = /* ... */;  \r\nvar p1: Promise<number> = /* ... */;  \r\nasync function fn() {  \r\n    try {  \r\n        await p0;  \r\n    }  \r\n    catch (e) {  \r\n        alert(e.message);  \r\n    }  \r\n    finally {  \r\n        await p1;  \r\n    }  \r\n}  \r\n  \r\n// async.js  \r\nvar __awaiter = /* ... */;  \r\nvar __generator = /* .. */;  \r\n  \r\nfunction fn() {  \r\n    var i;  \r\n    return new Promise(function (_resolve) {  \r\n        resolve(__awaiter(__generator(function (_state) {  \r\n            switch (_state.label) {  \r\n                case 0:  \r\n                    _state.trys = [];  \r\n                    _state.label = 1;  \r\n                case 1:  \r\n                    _state.trys.push([1, 3, 4, 6]);  \r\n                    return [3 /*yield*/, p0];  \r\n                case 2:  \r\n                    return [5 /*break*/, 6];  \r\n                case 3:  \r\n                    _a = _state.error;  \r\n                    alert(_a.message);  \r\n                    return [5 /*break*/, 6];  \r\n                case 4:  \r\n                    return [3 /*yield*/, p1];  \r\n                case 5:  \r\n                    return [6 /*endfinally*/];  \r\n                case 6:  \r\n                    return [2 /*return*/];  \r\n            }  \r\n        }));  \r\n    });  \r\n    var _a;  \r\n}\r\n```\r\n\r\nAs a result of these transformations, the JavaScript output for an *Async Function* can look quite different than the original source. When debugging the JavaScript output for an *Async Function* it would be advisable to use a Source Map generated using the `--sourceMap` option for the compiler.\r\n\r\n# <a name=\"4\"/>4 Promise\r\n\r\n*Async Functions* require a compatible Promise abstraction to operate properly. A compatible implementation implements the following interfaces, which are to be added to the core library declarations (lib.d.ts):\r\n\r\n```TypeScript\r\ninterface IPromiseConstructor<T> {  \r\n    new (init: (resolve: (value: T | IPromise<T>) => void, reject: (reason: any) => void) => void): IPromise<T>;  \r\n}  \r\n  \r\ninterface IPromise<T> {  \r\n    then<TResult>(onfulfilled: (value: T) => TResult | IPromise<TResult>, onrejected: (reason: any) => TResult | IPromise<TResult>): IPromise<TResult>;  \r\n}\r\n```\r\n\r\nThe following libraries contain compatible Promise implementations:\r\n\r\n* ES6 - http://people.mozilla.org/~jorendorff/es6-draft.html#sec-promise-objects\r\n* WinJS - http://msdn.microsoft.com/en-us/library/windows/apps/br211867.aspx\r\n* Q - https://github.com/kriskowal/q\r\n* RSVP - https://github.com/tildeio/rsvp.js/\r\n* Bluebird - https://github.com/petkaantonov/bluebird\r\n* es6-promise - https://github.com/jakearchibald/es6-promise\r\n* lie - https://github.com/calvinmetcalf/lie\r\n* when - https://github.com/cujojs/when\r\n* asyncjs - https://github.com/rbuckton/asyncjs\r\n\r\n# <a name=\"A\"/>A Grammar\r\n\r\n## <a name=\"A.1\"/>A.1 Types\r\n\r\n&emsp;&emsp;*CallSignature*<sub> [Await,AsyncParameter] </sub>*:  \r\n&emsp;&emsp;&emsp;TypeParameters*<sub> opt</sub>*&emsp;*`(`*&emsp;ParameterList*<sub> [?Await,?AsyncParameter]opt</sub>*&emsp;*`)`*&emsp;TypeAnnotation*<sub> opt</sub>*\r\n\r\n&emsp;&emsp;ParameterList*<sub> [Await,AsyncParameter]</sub> *:*  \r\n&emsp;&emsp;&emsp;*RequiredParameterList*<sub> [?Await]</sub>  \r\n&emsp;&emsp;&emsp;*OptionalParameterList*<sub> [?Await,?AsyncParameter]</sub>  \r\n&emsp;&emsp;&emsp;*RestParameter*<sub> [?Await]</sub>  \r\n&emsp;&emsp;&emsp;*RequiredParameterList*<sub> [?Await]</sub>&emsp;`,`&emsp;*OptionalParameterList*<sub> [?Await,?AsyncParameter]</sub>  \r\n&emsp;&emsp;&emsp;*RequiredParameterList*<sub> [?Await]</sub>&emsp;`,`&emsp;*RestParameter*<sub> [?Await]</sub>  \r\n&emsp;&emsp;&emsp;*OptionalParameterList*<sub> [?Await,?AsyncParameter]</sub>&emsp;`,`&emsp;*RestParameter*<sub> [?Await]</sub>  \r\n&emsp;&emsp;&emsp;*RequiredParameterList*<sub> [?Await]</sub>&emsp;`,`&emsp;*OptionalParameterList*<sub> [?Await,?AsyncParameter]</sub>&emsp;`,`&emsp;*RestParameter*<sub> [?Await]</sub>\r\n\r\n&emsp;&emsp;*RequiredParameterList*<sub> [Await] </sub>*:*  \r\n&emsp;&emsp;&emsp;*RequiredParameter*<sub> [?Await]</sub>  \r\n&emsp;&emsp;&emsp;*RequiredParameterList*<sub> [?Await]</sub>&emsp;`,`&emsp;*RequiredParameter*<sub> [?Await]</sub>\r\n\r\n&emsp;&emsp;*RequiredParameter*<sub> [Await] </sub>*:*  \r\n&emsp;&emsp;&emsp;*AccessibilityModifier*<sub> opt</sub>&emsp;*BindingIdentifier*<sub> [?Await]</sub>&emsp;*TypeAnnotation*<sub> opt</sub>  \r\n&emsp;&emsp;&emsp;*Identifier*&emsp;`:`&emsp;*StringLiteral*\r\n\r\n&emsp;&emsp;*OptionalParameterList*<sub> [Await,AsyncParameter] </sub>*:*  \r\n&emsp;&emsp;&emsp;*OptionalParameter*<sub> [?Await,?AsyncParameter]</sub>  \r\n&emsp;&emsp;&emsp;*OptionalParameterList*<sub> [?Await,?AsyncParameter]</sub>&emsp;`,`&emsp;*OptionalParameter*<sub> [?Await,?AsyncParameter]</sub>\r\n\r\n&emsp;&emsp;*OptionalParameter*<sub> [Await,AsyncParameter]</sub>*:*  \r\n&emsp;&emsp;&emsp;[+AsyncParameter] *AccessibilityModifier*<sub> opt</sub>&emsp;*BindingIdentifier*<sub> [Await]</sub>&emsp;`?`&emsp;*TypeAnnotation*<sub> opt</sub>  \r\n&emsp;&emsp;&emsp;[+AsyncParameter] *AccessibilityModifier*<sub> opt</sub>&emsp;*BindingIdentifier*<sub> [Await]</sub>&emsp;*TypeAnnotation*<sub> opt</sub>&emsp;*Initialiser*<sub> [In]</sub>  \r\n&emsp;&emsp;&emsp;[+AsyncParameter] *BindingIdentifier*<sub> [Await]</sub>&emsp;`?`&emsp;`:`&emsp;*StringLiteral*  \r\n&emsp;&emsp;&emsp;[~AsyncParameter] *AccessibilityModifier*<sub> opt</sub>&emsp;*BindingIdentifier*<sub> [?Await]</sub>&emsp;`?`&emsp;*TypeAnnotation*<sub> opt</sub>  \r\n&emsp;&emsp;&emsp;[~AsyncParameter] *AccessibilityModifier*<sub> opt</sub>&emsp;*BindingIdentifier*<sub> [?Await]</sub>&emsp;*TypeAnnotation*<sub> opt</sub>&emsp;*Initialiser*<sub> [In,?Await]</sub>  \r\n&emsp;&emsp;&emsp;[~AsyncParameter] *BindingIdentifier*<sub> [?Await]</sub>&emsp;`?`&emsp;`:`&emsp;*StringLiteral* \r\n\r\n&emsp;&emsp;*RestParameter*<sub> [Await]</sub>*:*  \r\n&emsp;&emsp;&emsp;`...`&emsp;*BindingIdentifier*<sub> [?Await]</sub>&emsp;*TypeAnnotation*<sub> opt</sub>\r\n\r\n## <a name=\"A.2\"/>A.2 Expressions\r\n\r\n&emsp;&emsp;*BindingIdentifier*<sub> [Await]</sub> : ( Modified )  \r\n&emsp;&emsp;&emsp;*Identifier*&emsp;but not `await`  \r\n&emsp;&emsp;&emsp;[~Await] `await`\r\n\r\n&emsp;&emsp;*PropertyAssignment*<sub> [Await]</sub> :  \r\n&emsp;&emsp;&emsp;*PropertyName*&emsp;`:`&emsp;*AssignmentExpression*<sub> [?Await]</sub>  \r\n&emsp;&emsp;&emsp;*PropertyName*&emsp;*CallSignature*&emsp;`{`&emsp;*FunctionBody*&emsp;`}`  \r\n&emsp;&emsp;&emsp;*GetAccessor*  \r\n&emsp;&emsp;&emsp;*SetAccessor*  \r\n&emsp;&emsp;&emsp;`async`&emsp;[no *LineTerminator* here]&emsp;*PropertyName*&emsp;*CallSignature*<sub> [Await,AsyncParameter]</sub>&emsp;`{`&emsp;*FunctionBody*<sub> [Await]</sub>&emsp;`}`\r\n\r\n&emsp;&emsp;*GetAccessor* :  \r\n&emsp;&emsp;&emsp;`get`&emsp;*PropertyName*&emsp;`(`&emsp;`)`&emsp;*TypeAnnotation<sub>opt</sub>*&emsp;`{`&emsp;*FunctionBody*&emsp;`}`  \r\n&emsp;&emsp;&emsp;`async`&emsp;[no *LineTerminator* here]&emsp;`get`&emsp;*PropertyName*&emsp;`(`&emsp;`)`&emsp;*TypeAnnotation*<sub> opt</sub>&emsp;`{`&emsp;*FunctionBody*<sub> [Await]</sub>&emsp;`}`\r\n\r\n&emsp;&emsp;*FunctionExpression*<sub> [Await]</sub> : ( Modified )  \r\n&emsp;&emsp;&emsp;`function`&emsp;*BindingIdentifier*<sub> [?Await]opt</sub>&emsp;*CallSignature*&emsp;`{`&emsp;*FunctionBody*&emsp;`}`  \r\n&emsp;&emsp;&emsp;`async`&emsp;[no *LineTerminator* here]&emsp;`function`&emsp;*BindingIdentifier*<sub> [?Await]opt</sub>&emsp;*CallSignature*<sub> [Await,AsyncParameter]</sub>&emsp;`{`&emsp;*FunctionBody*<sub> [Await]</sub>&emsp;`}`\r\n\r\n&emsp;&emsp;*AssignmentExpression*<sub> [Await]</sub> : ( Modified )  \r\n&emsp;&emsp;&emsp;...  \r\n&emsp;&emsp;&emsp;*ArrowFunctionExpression*<sub> [?Await]</sub>\r\n\r\n&emsp;&emsp;*ArrowFunctionExpression*<sub> [Await]</sub> :  \r\n&emsp;&emsp;&emsp;*ArrowFormalParameters*&emsp;`=>`&emsp;*Block*<sub> [?Await]</sub>  \r\n&emsp;&emsp;&emsp;*ArrowFormalParameters*&emsp;`=>`&emsp;*AssignmentExpression*<sub> [?Await]</sub>  \r\n&emsp;&emsp;&emsp;`async`&emsp;[no *LineTerminator* here]&emsp;*CallSignature*<sub> [Await,AsyncParameter]</sub>&emsp;`=>`&emsp;*Block*<sub> [Await]</sub>  \r\n&emsp;&emsp;&emsp;`async`&emsp;[no *LineTerminator* here]&emsp;*CallSignature*<sub> [Await,AsyncParameter]</sub>&emsp;`=>`&emsp;*AssignmentExpression*<sub> [Await]</sub>\r\n\r\n&emsp;&emsp;*ArrowFormalParameters*<sub> [Await]</sub> :  \r\n&emsp;&emsp;&emsp;*CallSignature*  \r\n&emsp;&emsp;&emsp;*BindingIdentifier*<sub> [?Await]</sub>\r\n\r\n&emsp;&emsp;*UnaryExpression*<sub> [Await]</sub> : ( Modified )  \r\n&emsp;&emsp;&emsp;...  \r\n&emsp;&emsp;&emsp;`<`&emsp;*Type*&emsp;`>`&emsp;*UnaryExpression*<sub> [?Await]</sub>  \r\n&emsp;&emsp;&emsp;[+Await]&emsp;`await`&emsp;*UnaryExpression*<sub> [Await]</sub>\r\n\r\n## <a name=\"A.3\"/>A.3 Functions\r\n\r\n&emsp;&emsp;*FunctionDeclaration*<sub> [Await]</sub> : ( Modified )  \r\n&emsp;&emsp;&emsp;*FunctionOverloads*<sub> [?Await]opt</sub>&emsp;*FunctionImplementation*<sub> [?Await]</sub>\r\n\r\n&emsp;&emsp;*FunctionOverloads*<sub> [Await]</sub> :  \r\n&emsp;&emsp;&emsp;*FunctionOverloads*<sub> [?Await]opt</sub>&emsp;*FunctionOverload*<sub> [?Await]</sub>\r\n\r\n&emsp;&emsp;*FunctionOverload*<sub> [Await]</sub> :  \r\n&emsp;&emsp;&emsp;`function`&emsp;*BindingIdentifier*<sub> [?Await]</sub>&emsp;*CallSignature*&emsp;`;`\r\n\r\n&emsp;&emsp;FunctionImplementation<sub> [Await]</sub> :  \r\n&emsp;&emsp;&emsp;`function`&emsp;*BindingIdentifier*<sub> [?Await]</sub>&emsp;*CallSignature*&emsp;`{`&emsp;*FunctionBody*&emsp;`}`  \r\n&emsp;&emsp;&emsp;`async`&emsp;[no *LineTerminator* here]&emsp;`function`&emsp;*BindingIdentifier*<sub> [?Await]</sub>&emsp;*CallSignature*<sub> [Await,AsyncParameter]</sub>&emsp;`{`&emsp;*FunctionBody*<sub> [Await]</sub>&emsp;`}`\r\n\r\n## <a name=\"A.4\"/>A.4 Classes\r\n\r\n&emsp;&emsp;*MemberFunctionImplementation*:  \r\n&emsp;&emsp;&emsp;*AccessibilityModifier*<sub>opt</sub>&emsp;`static`<sub> opt</sub>&emsp;*PropertyName*&emsp;*CallSignature*&emsp;`{`&emsp;*FunctionBody*&emsp;`}`  \r\n&emsp;&emsp;&emsp;*AccessibilityModifier*<sub>opt</sub>&emsp;`static`<sub> opt</sub>&emsp;`async`&emsp;[no *LineTerminator* here]&emsp;*PropertyName*&emsp;*CallSignature*<sub> [Await,AsyncParameter]</sub>&emsp;`{`&emsp;*FunctionBody*<sub> [Await]</sub>&emsp;`}` \r\n\r\n# <a name=\"B\"/>B Helper Functions\r\n\r\nThere are two helper functions that are used for *Async Functions*. The `__awaiter` helper function is used by both the ES6 as well as the ES5/ES3 transformations. The `__generator` helper function is used only by the ES5/ES3 transformation.\r\n\r\n## <a name=\"B.1\"/>B.1  `__awaiter`  helper function\r\n\r\n```TypeScript\r\nvar __awaiter = __awaiter || function (g) {  \r\n    function n(r, t) {  \r\n        while (true) {  \r\n            if (r.done) return r.value;  \r\n            if (r.value && typeof (t = r.value.then) === \"function\")  \r\n                return t.call(r.value, function (v) { return n(g.next(v)) }, function (v) { return n(g[\"throw\"](v)) });  \r\n            r = g.next(r.value);  \r\n        }  \r\n    }  \r\n    return n(g.next());  \r\n};\r\n```\r\n\r\n## <a name=\"B.2\"/>B.2  `__generator`  helper function\r\n\r\n```TypeScript\r\nvar __generator = __generator || function (m) {  \r\n    var d, i = [], f, g, s = { label: 0 }, y, b;  \r\n    function n(c) {  \r\n        if (f) throw new TypeError(\"Generator is already executing.\");  \r\n        switch (d && c[0]) {  \r\n            case 0 /*next*/: return { value: void 0, done: true };  \r\n            case 1 /*throw*/: throw c[1];  \r\n            case 2 /*return*/: return { value: c[1], done: true };  \r\n        }  \r\n        while (true) {  \r\n            f = false;  \r\n            switch (!(g = s.trys && s.trys.length && s.trys[s.trys.length - 1]) && c[0]) {  \r\n                case 1 /*throw*/: i.length = 0; d = true; throw c[1];  \r\n                case 2 /*return*/: i.length = 0; d = true; return { value: c[1], done: true };  \r\n            }  \r\n            try {  \r\n                if (y) {  \r\n                    f = true;  \r\n                    if (typeof (b = y[c[0]]) === \"function\") {  \r\n                        b = b.call(y, c[1]);  \r\n                        if (!b.done) return b;  \r\n                        c[0] = 0 /*next*/, c[1] = b.value;  \r\n                    }  \r\n                    y = undefined;  \r\n                    f = false;  \r\n                }  \r\n                switch (c[0]) {  \r\n                    case 0 /*next*/: s.sent = c[1]; break;  \r\n                    case 3 /*yield*/: s.label++; return { value: c[1], done: false };  \r\n                    case 4 /*yield**/: s.label++; y = c[1]; c[0] = 0 /*next*/; c[1] = void 0; continue;  \r\n                    case 6 /*endfinally*/: c = i.pop(); continue;  \r\n                    default:  \r\n                        if (c[0] === 1 /*throw*/ && s.label < g[1]) { s.error = c[1]; s.label = g[1]; break; }  \r\n                        if (c[0] === 5 /*break*/ && (!g || (c[1] >= g[0] && c[1] < g[3]))) { s.label = c[1]; break; }  \r\n                        s.trys.pop();  \r\n                        if (g[2]) { i.push(c); s.label = g[2]; break; }  \r\n                        continue;  \r\n                }  \r\n                f = true;  \r\n                c = m(s);  \r\n            } catch (e) {  \r\n                y = void 0;  \r\n                c[0] = 1 /*throw*/, c[1] = e;  \r\n            }  \r\n        }  \r\n    }  \r\n    return {  \r\n        next: function (v) { return n([0 /*next*/, v]); },  \r\n        1 /*throw*/: function (v) { return n([1 /*throw*/, v]); },  \r\n        2 /*return*/: function (v) { return n([2 /*return*/, v]); },  \r\n    };  \r\n};\r\n```\r\n\r\n### <a name=\"B.2.1\"/>B.2.1 `_generator`  Arguments\r\n\r\n|argument|description|\r\n|---|---|\r\n|`m`|State machine function.|\r\n\r\n### <a name=\"B.2.2\"/>B.2.2  `n`  Arguments\r\n\r\n|argument |description |\r\n|---|---|\r\n|`c`|The current instruction.|\r\n\r\n### <a name=\"B.2.3\"/>B.2.3 Variables\r\n\r\n|variable |description |\r\n|---|---|\r\n|`d`|A value indicating whether the generator is done executing.|\r\n|`i`|A stack of instructions (see below) pending execution.|\r\n|`f`|A value indicating whether the generator is executing, to prevent reentry (per ES6 spec).|\r\n|`g`|The region at the top of the `s.trys` stack.|\r\n|`s`|State information for the state machine function.|\r\n|`s.label`|The label for the next instruction.|\r\n|`s.trys`|An optional stack of protected regions for `try..catch..finally` blocks.|\r\n|`s.sent`|A value sent to the generator when calling `next`.|\r\n|`s.error`|An error caught by a `catch` clause.|\r\n|`y`|The current inner generator to which to delegate generator instructions.|\r\n|`b`|The method on `n` to execute for the current instruction. One of `\"next\"`, `\"throw\"`, or `\"return\"`.|\r\n\r\n### <a name=\"B.2.4\"/>B.2.4 Instructions\r\n\r\n|instruction |args|description|\r\n|---|:---:|---|\r\n|`0 /*next*/`|0-1|Begin or resume processing with an optional sent value.|\r\n|`1 /*throw*/`|1|Throw an exception at the current instruction. Executes any enclosing `catch` or `finally` blocks.|\r\n|`2 /*return*/`|0-1|Returns a value from the current instruction. Executes any enclosing `finally` blocks.|\r\n|`3 /*yield*/`|0-1|Suspends execution and yields an optional value to the caller of the generator.|\r\n|`4 /*yield**/`|1|Delegates generator operations to the provided iterable (not needed for *Async Functions*, but provided for future support for down-level *Generator Functions*).|\r\n|`5 /*break*/`|1|Jumps to a labeled instruction. Executes any enclosing `finally` blocks if the target is outside of the current protected region.|\r\n|`6 /*endfinally*/`|0|Marks the end of a `finally` block so that the previous `break`, `throw`, or `return` instruction can be processed.|\r\n\r\n### <a name=\"B.2.5\"/>B.2.5 Protected Regions\r\n\r\nA protected region marks the beginning and end of a `try..catch` or `try..finally` block. Protected regions are pushed onto the `s.trys` stack whenever a protected region is entered when executing the state machine. A protected region is defined using a quadruple in the following format:\r\n\r\n|field |required|description|\r\n|---|---|---|\r\n|0|yes|The start of a `try` block.|\r\n|1|no|The start of a `catch` block.|\r\n|2|no|The start of a `finally` block.|\r\n|3|yes|The end of a `try..catch..finally` block.|\r\n\r\n \r\n\r\n## <a name=\"B.3\"/>B.3 `__generator` helper function (alternate)\r\n\r\n```TypeScript\r\nvar __generator = __generator || function (body) {  \r\n    var done, instructions = [], stepping, region, state = { label: 0 }, delegated;  \r\n    function step(instruction) {  \r\n        if (stepping) throw new TypeError(\"Generator is already executing.\");  \r\n        if (done) {  \r\n            switch (instruction[0]) {  \r\n                case 0 /*next*/: return { value: void 0, done: true };  \r\n                case 1 /*throw*/: throw instruction[1];  \r\n                case 2 /*return*/: return { value: instruction[1], done: true };  \r\n            }  \r\n        }  \r\n        while (true) {  \r\n            stepping = false;  \r\n            var region = state.trys && state.trys.length && state.trys[state.trys.length - 1];  \r\n            if (region) {  \r\n                switch (instruction[0]) {  \r\n                    case 1 /*throw*/:  \r\n                        instructions.length = 0;  \r\n                        done = true;  \r\n                        throw instruction[1];  \r\n                    case 2 /*return*/:  \r\n                        instructions.length = 0;  \r\n                        done = true;  \r\n                        return { value: instruction[1], done: true };  \r\n                }  \r\n            }  \r\n            try {  \r\n                if (delegated) {  \r\n                    stepping = true;  \r\n                    var callback = delegated[instruction[0]];  \r\n                    if (typeof callback === \"function\") {  \r\n                        var result = callback.call(delegated, instruction[1]);  \r\n                        if (!result.done) return result;  \r\n                        instruction[0] = 0 /*next*/;  \r\n                        instruction[1] = result.value;  \r\n                    }  \r\n                    delegated = undefined;  \r\n                    stepping = false;  \r\n                }  \r\n                switch (instruction[0]) {  \r\n                    case 3 /*yield*/:  \r\n                        state.label++;  \r\n                        return { value: instruction[1], done: false };  \r\n                    case 4 /*yield**/:  \r\n                        state.label++;  \r\n                        delegated = instruction[1];  \r\n                        instruction[0] = 0 /*next*/;  \r\n                        instruction[1] = void 0;  \r\n                        continue;  \r\n                    case 0 /*next*/:  \r\n                        state.sent = instruction[1];  \r\n                        break;  \r\n                    case 6 /*endfinally*/:  \r\n                        instruction = instructions.pop();  \r\n                        continue;  \r\n                    default:  \r\n                        if (instruction[0] === 5 /*break*/ && (!region || (instruction[1] >= region[0] && instruction[1] < region[3]))) {  \r\n                            state.label = instruction[1];  \r\n                            break;  \r\n                        }  \r\n                        if (instruction[0] === 1 /*throw*/ && state.label < region[1]) {  \r\n                            state.error = instruction[1];  \r\n                            state.label = region[1];  \r\n                            break;  \r\n                        }  \r\n                        state.trys.pop();  \r\n                        if (region[2]) {  \r\n                            instructions.push(instruction);  \r\n                            state.label = region[2];  \r\n                            break;  \r\n                        }  \r\n                        continue;  \r\n                }  \r\n                stepping = true;  \r\n                instruction = body(state);  \r\n            } catch (e) {  \r\n                delegated = void 0;  \r\n                instruction[0] = 1 /*throw*/, instruction[1] = e;  \r\n            }  \r\n        }  \r\n    }  \r\n    return {  \r\n        next: function (v) { return step([0 /*next*/, v]); },  \r\n        \"throw\": function (v) { return step([1 /*throw*/, v]); },  \r\n        \"return\": function (v) { return step([2 /*return*/, v]); },  \r\n    };  \r\n};\r\n```"},"comment":{"url":"https://api.github.com/repos/Microsoft/TypeScript/issues/comments/146292873","html_url":"https://github.com/Microsoft/TypeScript/issues/1664#issuecomment-146292873","issue_url":"https://api.github.com/repos/Microsoft/TypeScript/issues/1664","id":146292873,"user":{"login":"bterlson","id":11236,"avatar_url":"https://avatars.githubusercontent.com/u/11236?v=3","gravatar_id":"","url":"https://api.github.com/users/bterlson","html_url":"https://github.com/bterlson","followers_url":"https://api.github.com/users/bterlson/followers","following_url":"https://api.github.com/users/bterlson/following{/other_user}","gists_url":"https://api.github.com/users/bterlson/gists{/gist_id}","starred_url":"https://api.github.com/users/bterlson/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/bterlson/subscriptions","organizations_url":"https://api.github.com/users/bterlson/orgs","repos_url":"https://api.github.com/users/bterlson/repos","events_url":"https://api.github.com/users/bterlson/events{/privacy}","received_events_url":"https://api.github.com/users/bterlson/received_events","type":"User","site_admin":false},"created_at":"2015-10-07T18:54:31Z","updated_at":"2015-10-07T18:54:31Z","body":"`await *` is not part of the proposal. It is not worth adding the syntax as sugar for `Promise.all(` + `)` and it is not analogous with yield * so is confusing in that respect as well. It is better to save this syntax for a proposal that will have a bigger impact on async programming than saving a few characters."}}