Title: Proposal: Async Functions · Issue #1664 · microsoft/TypeScript · GitHub
Open Graph Title: Proposal: Async Functions · Issue #1664 · microsoft/TypeScript
X Title: Proposal: Async Functions · Issue #1664 · microsoft/TypeScript
Description: Async Functions 1 Async Functions This is a spec proposal for the addition of Async Functions (also known as async..await) as a feature of TypeScript. 2 Use Cases Async Functions allow TypeScript developers to author functions that are e...
Open Graph Description: Async Functions 1 Async Functions This is a spec proposal for the addition of Async Functions (also known as async..await) as a feature of TypeScript. 2 Use Cases Async Functions allow TypeScript d...
X Description: Async Functions 1 Async Functions This is a spec proposal for the addition of Async Functions (also known as async..await) as a feature of TypeScript. 2 Use Cases Async Functions allow TypeScript d...
Opengraph URL: https://github.com/microsoft/TypeScript/issues/1664
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Proposal: Async Functions","articleBody":"# Async Functions\n# \u003ca name=\"1\"/>1 Async Functions\n\nThis is a spec proposal for the addition of _Async Functions_ (also known as `async..await`) as a feature of TypeScript.\n# \u003ca name=\"2\"/>2 Use Cases\n\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.\n\nThis is based primarily on the Async Functions strawman proposal for ECMAScript, and C# 5.0 § 10.15 _Async Functions_.\n# \u003ca name=\"3\"/>3 Introduction\n## \u003ca name=\"3.1\"/>3.1 Syntax\n\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.\n\nExample:\n\n``` TypeScript\nvar p: Promise\u003cnumber> = /* ... */; \nasync function fn(): Promise\u003cnumber> { \n var i = await p; // suspend execution until 'p' is settled. 'i' has type \"number\" \n return 1 + i; \n} \n\nvar a = async (): Promise\u003cnumber> => 1 + await p; // suspends execution. \nvar a = async () => 1 + await p; // suspends execution. return type is inferred as \"Promise\u003cnumber>\" when compiling with --target ES6 \nvar fe = async function(): Promise\u003cnumber> { \n var i = await p; // suspend execution until 'p' is settled. 'i' has type \"number\" \n return 1 + i; \n} \n\nclass C { \n async m(): Promise\u003cnumber> { \n var i = await p; // suspend execution until 'p' is settled. 'i' has type \"number\" \n return 1 + i; \n } \n\n async get p(): Promise\u003cnumber> { \n var i = await p; // suspend execution until 'p' is settled. 'i' has type \"number\" \n return 1 + i; \n } \n}\n```\n## \u003ca name=\"3.2\"/>3.2 Transformations\n\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.\n### \u003ca name=\"3.2.1\"/>3.2.1 ES6 Transformations\n\nDuring compilation of an _Async Function_ when targeting ES6, the following transformations are applied:\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_.\n- The original function body is enclosed in a _Generator Function_.\n- Any `await` expressions inside the original function body are transformed into a compatible `yield` expression.\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.\n- The _Generator Function_ is executed and the resulting generator is passed as an argument to the `__awaiter` helper function.\n- The result of the `__awaiter` helper function is passed as an argument to the promise resolve callback.\n\nExample:\n\n``` TypeScript\n// async.ts \nvar p0: Promise\u003cnumber> = /* ... */; \nvar p1: Promise\u003cnumber> = /* ... */; \nasync function fn() { \n var i = await p0; \n return await p1 + i; \n} \n\n// async.js \nvar __awaiter = /* ... */; \n\nfunction fn() { \n return new Promise(function (_resolve) { \n _resolve(__awaiter(function* () { \n var i = yield p0; \n return (yield p1) + i; \n }())); \n }); \n}\n```\n### \u003ca name=\"3.2.2\"/>3.2.2 ES5/ES3 Transformations\n\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:\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_.\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.\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.\n- The original function body is rewritten into a series of case clauses in a switch statement.\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.\n- Temporary locals are generated to hold onto the values for partially-applied expressions to preserve side effects expected in the original source.\n- Logical binary expressions that contain an `await` expression are rewritten to preserve shortcutting.\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.\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.\n- New expressions that contain an `await` in the argument list are rewritten to preserve side effects.\n- Array literal expressions that contain an `await` in the element list are rewritten to preserve side effects.\n- Object literal expressions that contain an `await` in the element list are rewritten to preserve side effects.\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.\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.\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.\n- `return` statements are rewritten to return an instruction interpreted by the `__generator` helper function.\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.\n- Labeled statements that contain an `await` expression are rewritten.\n- `await` expressions are rewritten to return an instruction interpreted by the `__generator` helper function.\n\nExample:\n\n``` TypeScript\n// async.ts \nvar p0: Promise\u003cnumber> = /* ... */; \nvar p1: Promise\u003cnumber> = /* ... */; \nasync function fn() { \n var i = await p0; \n return await p1 + i; \n} \n\n// async.js \nvar __awaiter = /* ... */; \nvar __generator = /* ... */; \n\nfunction fn() { \n var i; \n return new Promise(function (_resolve) { \n resolve(__awaiter(__generator(function (_state) { \n switch (_state.label) { \n case 0: return [3 /*yield*/, p0]; \n case 1: \n i = _state.sent; \n return [3 /*yield*/, p1]; \n case 2: \n return [2 /*return*/, _state.sent + i]; \n } \n })); \n }); \n}\n```\n\nThe following is an example of an async function that contains a `try` statement:\n\n``` TypeScript\n// async.ts \nvar p0: Promise\u003cnumber> = /* ... */; \nvar p1: Promise\u003cnumber> = /* ... */; \nasync function fn() { \n try { \n await p0; \n } \n catch (e) { \n alert(e.message); \n } \n finally { \n await p1; \n } \n} \n\n// async.js \nvar __awaiter = /* ... */; \nvar __generator = /* .. */; \n\nfunction fn() { \n var i; \n return new Promise(function (_resolve) { \n resolve(__awaiter(__generator(function (_state) { \n switch (_state.label) { \n case 0: \n _state.trys = []; \n _state.label = 1; \n case 1: \n _state.trys.push([1, 3, 4, 6]); \n return [3 /*yield*/, p0]; \n case 2: \n return [5 /*break*/, 6]; \n case 3: \n _a = _state.error; \n alert(_a.message); \n return [5 /*break*/, 6]; \n case 4: \n return [3 /*yield*/, p1]; \n case 5: \n return [6 /*endfinally*/]; \n case 6: \n return [2 /*return*/]; \n } \n })); \n }); \n var _a; \n}\n```\n\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.\n# \u003ca name=\"4\"/>4 Promise\n\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):\n\n``` TypeScript\ninterface IPromiseConstructor\u003cT> { \n new (init: (resolve: (value: T | IPromise\u003cT>) => void, reject: (reason: any) => void) => void): IPromise\u003cT>; \n} \n\ninterface IPromise\u003cT> { \n then\u003cTResult>(onfulfilled: (value: T) => TResult | IPromise\u003cTResult>, onrejected: (reason: any) => TResult | IPromise\u003cTResult>): IPromise\u003cTResult>; \n}\n```\n\nThe following libraries contain compatible Promise implementations:\n- ES6 - http://people.mozilla.org/~jorendorff/es6-draft.html#sec-promise-objects\n- WinJS - http://msdn.microsoft.com/en-us/library/windows/apps/br211867.aspx\n- Q - https://github.com/kriskowal/q\n- RSVP - https://github.com/tildeio/rsvp.js/\n- Bluebird - https://github.com/petkaantonov/bluebird\n- es6-promise - https://github.com/jakearchibald/es6-promise\n- lie - https://github.com/calvinmetcalf/lie\n- when - https://github.com/cujojs/when\n- asyncjs - https://github.com/rbuckton/asyncjs\n# \u003ca name=\"A\"/>A Grammar\n## \u003ca name=\"A.1\"/>A.1 Types\n\n _CallSignature_\u003csub> [Await,AsyncParameter] \u003c/sub>_: \n TypeParameters_\u003csub> opt\u003c/sub>_ _`(`_ ParameterList_\u003csub> [?Await,?AsyncParameter]opt\u003c/sub>_ _`)`_ TypeAnnotation_\u003csub> opt\u003c/sub>*\n\n ParameterList_\u003csub> [Await,AsyncParameter]\u003c/sub> *:_ \n _RequiredParameterList_\u003csub> [?Await]\u003c/sub> \n _OptionalParameterList_\u003csub> [?Await,?AsyncParameter]\u003c/sub> \n _RestParameter_\u003csub> [?Await]\u003c/sub> \n _RequiredParameterList_\u003csub> [?Await]\u003c/sub> `,` _OptionalParameterList_\u003csub> [?Await,?AsyncParameter]\u003c/sub> \n _RequiredParameterList_\u003csub> [?Await]\u003c/sub> `,` _RestParameter_\u003csub> [?Await]\u003c/sub> \n _OptionalParameterList_\u003csub> [?Await,?AsyncParameter]\u003c/sub> `,` _RestParameter_\u003csub> [?Await]\u003c/sub> \n _RequiredParameterList_\u003csub> [?Await]\u003c/sub> `,` _OptionalParameterList_\u003csub> [?Await,?AsyncParameter]\u003c/sub> `,` _RestParameter_\u003csub> [?Await]\u003c/sub>\n\n _RequiredParameterList_\u003csub> [Await] \u003c/sub>_:_ \n _RequiredParameter_\u003csub> [?Await]\u003c/sub> \n _RequiredParameterList_\u003csub> [?Await]\u003c/sub> `,` _RequiredParameter_\u003csub> [?Await]\u003c/sub>\n\n _RequiredParameter_\u003csub> [Await] \u003c/sub>_:_ \n _AccessibilityModifier_\u003csub> opt\u003c/sub> _BindingIdentifier_\u003csub> [?Await]\u003c/sub> _TypeAnnotation_\u003csub> opt\u003c/sub> \n _Identifier_ `:` _StringLiteral_\n\n _OptionalParameterList_\u003csub> [Await,AsyncParameter] \u003c/sub>_:_ \n _OptionalParameter_\u003csub> [?Await,?AsyncParameter]\u003c/sub> \n _OptionalParameterList_\u003csub> [?Await,?AsyncParameter]\u003c/sub> `,` _OptionalParameter_\u003csub> [?Await,?AsyncParameter]\u003c/sub>\n\n _OptionalParameter_\u003csub> [Await,AsyncParameter]\u003c/sub>_:_ \n [+AsyncParameter] _AccessibilityModifier_\u003csub> opt\u003c/sub> _BindingIdentifier_\u003csub> [Await]\u003c/sub> `?` _TypeAnnotation_\u003csub> opt\u003c/sub> \n [+AsyncParameter] _AccessibilityModifier_\u003csub> opt\u003c/sub> _BindingIdentifier_\u003csub> [Await]\u003c/sub> _TypeAnnotation_\u003csub> opt\u003c/sub> _Initialiser_\u003csub> [In]\u003c/sub> \n [+AsyncParameter] _BindingIdentifier_\u003csub> [Await]\u003c/sub> `?` `:` _StringLiteral_ \n [~AsyncParameter] _AccessibilityModifier_\u003csub> opt\u003c/sub> _BindingIdentifier_\u003csub> [?Await]\u003c/sub> `?` _TypeAnnotation_\u003csub> opt\u003c/sub> \n [~AsyncParameter] _AccessibilityModifier_\u003csub> opt\u003c/sub> _BindingIdentifier_\u003csub> [?Await]\u003c/sub> _TypeAnnotation_\u003csub> opt\u003c/sub> _Initialiser_\u003csub> [In,?Await]\u003c/sub> \n [~AsyncParameter] _BindingIdentifier_\u003csub> [?Await]\u003c/sub> `?` `:` _StringLiteral_ \n\n _RestParameter_\u003csub> [Await]\u003c/sub>_:_ \n `...` _BindingIdentifier_\u003csub> [?Await]\u003c/sub> _TypeAnnotation_\u003csub> opt\u003c/sub>\n## \u003ca name=\"A.2\"/>A.2 Expressions\n\n _BindingIdentifier_\u003csub> [Await]\u003c/sub> : ( Modified ) \n _Identifier_ but not `await` \n [~Await] `await`\n\n _PropertyAssignment_\u003csub> [Await]\u003c/sub> : \n _PropertyName_ `:` _AssignmentExpression_\u003csub> [?Await]\u003c/sub> \n _PropertyName_ _CallSignature_ `{` _FunctionBody_ `}` \n _GetAccessor_ \n _SetAccessor_ \n `async` [no _LineTerminator_ here] _PropertyName_ _CallSignature_\u003csub> [Await,AsyncParameter]\u003c/sub> `{` _FunctionBody_\u003csub> [Await]\u003c/sub> `}`\n\n _GetAccessor_ : \n `get` _PropertyName_ `(` `)` _TypeAnnotation\u003csub>opt\u003c/sub>_ `{` _FunctionBody_ `}` \n `async` [no _LineTerminator_ here] `get` _PropertyName_ `(` `)` _TypeAnnotation_\u003csub> opt\u003c/sub> `{` _FunctionBody_\u003csub> [Await]\u003c/sub> `}`\n\n _FunctionExpression_\u003csub> [Await]\u003c/sub> : ( Modified ) \n `function` _BindingIdentifier_\u003csub> [?Await]opt\u003c/sub> _CallSignature_ `{` _FunctionBody_ `}` \n `async` [no _LineTerminator_ here] `function` _BindingIdentifier_\u003csub> [?Await]opt\u003c/sub> _CallSignature_\u003csub> [Await,AsyncParameter]\u003c/sub> `{` _FunctionBody_\u003csub> [Await]\u003c/sub> `}`\n\n _AssignmentExpression_\u003csub> [Await]\u003c/sub> : ( Modified ) \n ... \n _ArrowFunctionExpression_\u003csub> [?Await]\u003c/sub>\n\n _ArrowFunctionExpression_\u003csub> [Await]\u003c/sub> : \n _ArrowFormalParameters_ `=>` _Block_\u003csub> [?Await]\u003c/sub> \n _ArrowFormalParameters_ `=>` _AssignmentExpression_\u003csub> [?Await]\u003c/sub> \n `async` [no _LineTerminator_ here] _CallSignature_\u003csub> [Await,AsyncParameter]\u003c/sub> `=>` _Block_\u003csub> [Await]\u003c/sub> \n `async` [no _LineTerminator_ here] _CallSignature_\u003csub> [Await,AsyncParameter]\u003c/sub> `=>` _AssignmentExpression_\u003csub> [Await]\u003c/sub>\n\n _ArrowFormalParameters_\u003csub> [Await]\u003c/sub> : \n _CallSignature_ \n _BindingIdentifier_\u003csub> [?Await]\u003c/sub>\n\n _UnaryExpression_\u003csub> [Await]\u003c/sub> : ( Modified ) \n ... \n `\u003c` _Type_ `>` _UnaryExpression_\u003csub> [?Await]\u003c/sub> \n [+Await] `await` _UnaryExpression_\u003csub> [Await]\u003c/sub>\n## \u003ca name=\"A.3\"/>A.3 Functions\n\n _FunctionDeclaration_\u003csub> [Await]\u003c/sub> : ( Modified ) \n _FunctionOverloads_\u003csub> [?Await]opt\u003c/sub> _FunctionImplementation_\u003csub> [?Await]\u003c/sub>\n\n _FunctionOverloads_\u003csub> [Await]\u003c/sub> : \n _FunctionOverloads_\u003csub> [?Await]opt\u003c/sub> _FunctionOverload_\u003csub> [?Await]\u003c/sub>\n\n _FunctionOverload_\u003csub> [Await]\u003c/sub> : \n `function` _BindingIdentifier_\u003csub> [?Await]\u003c/sub> _CallSignature_ `;`\n\n FunctionImplementation\u003csub> [Await]\u003c/sub> : \n `function` _BindingIdentifier_\u003csub> [?Await]\u003c/sub> _CallSignature_ `{` _FunctionBody_ `}` \n `async` [no _LineTerminator_ here] `function` _BindingIdentifier_\u003csub> [?Await]\u003c/sub> _CallSignature_\u003csub> [Await,AsyncParameter]\u003c/sub> `{` _FunctionBody_\u003csub> [Await]\u003c/sub> `}`\n## \u003ca name=\"A.4\"/>A.4 Classes\n\n _MemberFunctionImplementation_: \n _AccessibilityModifier_\u003csub>opt\u003c/sub> `static`\u003csub> opt\u003c/sub> _PropertyName_ _CallSignature_ `{` _FunctionBody_ `}` \n _AccessibilityModifier_\u003csub>opt\u003c/sub> `static`\u003csub> opt\u003c/sub> `async` [no _LineTerminator_ here] _PropertyName_ _CallSignature_\u003csub> [Await,AsyncParameter]\u003c/sub> `{` _FunctionBody_\u003csub> [Await]\u003c/sub> `}` \n# \u003ca name=\"B\"/>B Helper Functions\n\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.\n## \u003ca name=\"B.1\"/>B.1 `__awaiter` helper function\n\n``` TypeScript\nvar __awaiter = __awaiter || function (g) { \n function n(r, t) { \n while (true) { \n if (r.done) return r.value; \n if (r.value && typeof (t = r.value.then) === \"function\") \n return t.call(r.value, function (v) { return n(g.next(v)) }, function (v) { return n(g[\"throw\"](v)) }); \n r = g.next(r.value); \n } \n } \n return n(g.next()); \n};\n```\n## \u003ca name=\"B.2\"/>B.2 `__generator` helper function\n\n``` TypeScript\nvar __generator = __generator || function (m) { \n var d, i = [], f, g, s = { label: 0 }, y, b; \n function n(c) { \n if (f) throw new TypeError(\"Generator is already executing.\"); \n switch (d && c[0]) { \n case 0 /*next*/: return { value: void 0, done: true }; \n case 1 /*throw*/: throw c[1]; \n case 2 /*return*/: return { value: c[1], done: true }; \n } \n while (true) { \n f = false; \n switch (!(g = s.trys && s.trys.length && s.trys[s.trys.length - 1]) && c[0]) { \n case 1 /*throw*/: i.length = 0; d = true; throw c[1]; \n case 2 /*return*/: i.length = 0; d = true; return { value: c[1], done: true }; \n } \n try { \n if (y) { \n f = true; \n if (typeof (b = y[c[0]]) === \"function\") { \n b = b.call(y, c[1]); \n if (!b.done) return b; \n c[0] = 0 /*next*/, c[1] = b.value; \n } \n y = undefined; \n f = false; \n } \n switch (c[0]) { \n case 0 /*next*/: s.sent = c[1]; break; \n case 3 /*yield*/: s.label++; return { value: c[1], done: false }; \n case 4 /*yield**/: s.label++; y = c[1]; c[0] = 0 /*next*/; c[1] = void 0; continue; \n case 6 /*endfinally*/: c = i.pop(); continue; \n default: \n if (c[0] === 1 /*throw*/ && s.label \u003c g[1]) { s.error = c[1]; s.label = g[1]; break; } \n if (c[0] === 5 /*break*/ && (!g || (c[1] >= g[0] && c[1] \u003c g[3]))) { s.label = c[1]; break; } \n s.trys.pop(); \n if (g[2]) { i.push(c); s.label = g[2]; break; } \n continue; \n } \n f = true; \n c = m(s); \n } catch (e) { \n y = void 0; \n c[0] = 1 /*throw*/, c[1] = e; \n } \n } \n } \n return { \n next: function (v) { return n([0 /*next*/, v]); }, \n 1 /*throw*/: function (v) { return n([1 /*throw*/, v]); }, \n 2 /*return*/: function (v) { return n([2 /*return*/, v]); }, \n }; \n};\n```\n### \u003ca name=\"B.2.1\"/>B.2.1 `_generator` Arguments\n\n| argument | description |\n| --- | --- |\n| `m` | State machine function. |\n### \u003ca name=\"B.2.2\"/>B.2.2 `n` Arguments\n\n| argument | description |\n| --- | --- |\n| `c` | The current instruction. |\n### \u003ca name=\"B.2.3\"/>B.2.3 Variables\n\n| variable | description |\n| --- | --- |\n| `d` | A value indicating whether the generator is done executing. |\n| `i` | A stack of instructions (see below) pending execution. |\n| `f` | A value indicating whether the generator is executing, to prevent reentry (per ES6 spec). |\n| `g` | The region at the top of the `s.trys` stack. |\n| `s` | State information for the state machine function. |\n| `s.label` | The label for the next instruction. |\n| `s.trys` | An optional stack of protected regions for `try..catch..finally` blocks. |\n| `s.sent` | A value sent to the generator when calling `next`. |\n| `s.error` | An error caught by a `catch` clause. |\n| `y` | The current inner generator to which to delegate generator instructions. |\n| `b` | The method on `n` to execute for the current instruction. One of `\"next\"`, `\"throw\"`, or `\"return\"`. |\n### \u003ca name=\"B.2.4\"/>B.2.4 Instructions\n\n| instruction | args | description |\n| --- | :-: | --- |\n| `0 /*next*/` | 0-1 | Begin or resume processing with an optional sent value. |\n| `1 /*throw*/` | 1 | Throw an exception at the current instruction. Executes any enclosing `catch` or `finally` blocks. |\n| `2 /*return*/` | 0-1 | Returns a value from the current instruction. Executes any enclosing `finally` blocks. |\n| `3 /*yield*/` | 0-1 | Suspends execution and yields an optional value to the caller of the generator. |\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_). |\n| `5 /*break*/` | 1 | Jumps to a labeled instruction. Executes any enclosing `finally` blocks if the target is outside of the current protected region. |\n| `6 /*endfinally*/` | 0 | Marks the end of a `finally` block so that the previous `break`, `throw`, or `return` instruction can be processed. |\n### \u003ca name=\"B.2.5\"/>B.2.5 Protected Regions\n\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:\n\n| field | required | description |\n| --- | --- | --- |\n| 0 | yes | The start of a `try` block. |\n| 1 | no | The start of a `catch` block. |\n| 2 | no | The start of a `finally` block. |\n| 3 | yes | The end of a `try..catch..finally` block. |\n## \u003ca name=\"B.3\"/>B.3 `__generator` helper function (alternate)\n\n``` TypeScript\nvar __generator = __generator || function (body) { \n var done, instructions = [], stepping, region, state = { label: 0 }, delegated; \n function step(instruction) { \n if (stepping) throw new TypeError(\"Generator is already executing.\"); \n if (done) { \n switch (instruction[0]) { \n case 0 /*next*/: return { value: void 0, done: true }; \n case 1 /*throw*/: throw instruction[1]; \n case 2 /*return*/: return { value: instruction[1], done: true }; \n } \n } \n while (true) { \n stepping = false; \n var region = state.trys && state.trys.length && state.trys[state.trys.length - 1]; \n if (region) { \n switch (instruction[0]) { \n case 1 /*throw*/: \n instructions.length = 0; \n done = true; \n throw instruction[1]; \n case 2 /*return*/: \n instructions.length = 0; \n done = true; \n return { value: instruction[1], done: true }; \n } \n } \n try { \n if (delegated) { \n stepping = true; \n var callback = delegated[instruction[0]]; \n if (typeof callback === \"function\") { \n var result = callback.call(delegated, instruction[1]); \n if (!result.done) return result; \n instruction[0] = 0 /*next*/; \n instruction[1] = result.value; \n } \n delegated = undefined; \n stepping = false; \n } \n switch (instruction[0]) { \n case 3 /*yield*/: \n state.label++; \n return { value: instruction[1], done: false }; \n case 4 /*yield**/: \n state.label++; \n delegated = instruction[1]; \n instruction[0] = 0 /*next*/; \n instruction[1] = void 0; \n continue; \n case 0 /*next*/: \n state.sent = instruction[1]; \n break; \n case 6 /*endfinally*/: \n instruction = instructions.pop(); \n continue; \n default: \n if (instruction[0] === 5 /*break*/ && (!region || (instruction[1] >= region[0] && instruction[1] \u003c region[3]))) { \n state.label = instruction[1]; \n break; \n } \n if (instruction[0] === 1 /*throw*/ && state.label \u003c region[1]) { \n state.error = instruction[1]; \n state.label = region[1]; \n break; \n } \n state.trys.pop(); \n if (region[2]) { \n instructions.push(instruction); \n state.label = region[2]; \n break; \n } \n continue; \n } \n stepping = true; \n instruction = body(state); \n } catch (e) { \n delegated = void 0; \n instruction[0] = 1 /*throw*/, instruction[1] = e; \n } \n } \n } \n return { \n next: function (v) { return step([0 /*next*/, v]); }, \n \"throw\": function (v) { return step([1 /*throw*/, v]); }, \n \"return\": function (v) { return step([2 /*return*/, v]); }, \n }; \n};\n```\n","author":{"url":"https://github.com/rbuckton","@type":"Person","name":"rbuckton"},"datePublished":"2015-01-14T02:25:40Z","url":"https://github.com/microsoft/TypeScript/issues/1664"}
| react-profiling | 0 |
| react-import-map | react,react-dom,react-dom/client,react-dom/profiling,react-is,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@b8488ee2c322 |
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:4b12c415-04d5-0e2a-6ec0-0972c7a4fdb6 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | 9956:269AD9:F79AD1:14B8B83:6B010EF5 |
| html-safe-nonce | c09eedc81282ee1cec7281a3ae32582634cae43c5508bd3ed8054205155b7781 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI5OTU2OjI2OUFEOTpGNzlBRDE6MTRCOEI4Mzo2QjAxMEVGNSIsInZpc2l0b3JfaWQiOiI2ODEzMzI3NzM2OTQ2MzY0MTQ5IiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0= |
| visitor-hmac | 854044e8ccf9c52def50fbf30c65d57bcdad44e9829750803e498bced18c2d0a |
| hovercard-subject-tag | issue:54276542 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/microsoft/TypeScript/1664/issue_layout |
| twitter:image | https://opengraph.githubassets.com/c84a33a7e7afa2b525f7df4cb20e4df8cc6aa2e7ca1eba7609e22da2bc0fc27a/microsoft/TypeScript/issues/1664 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/c84a33a7e7afa2b525f7df4cb20e4df8cc6aa2e7ca1eba7609e22da2bc0fc27a/microsoft/TypeScript/issues/1664 |
| og:image:alt | Async Functions 1 Async Functions This is a spec proposal for the addition of Async Functions (also known as async..await) as a feature of TypeScript. 2 Use Cases Async Functions allow TypeScript d... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | rbuckton |
| hostname | github.com |
| expected-hostname | github.com |
| None | cb7fa109eca7d4156c889f474306b0e91336055e9adf8fd4684b61e49094d124 |
| turbo-cache-control | no-preview |
| go-import | github.com/microsoft/TypeScript git https://github.com/microsoft/TypeScript.git |
| octolytics-dimension-user_id | 6154722 |
| octolytics-dimension-user_login | microsoft |
| octolytics-dimension-repository_id | 20929025 |
| octolytics-dimension-repository_nwo | microsoft/TypeScript |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 20929025 |
| octolytics-dimension-repository_network_root_nwo | microsoft/TypeScript |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 2b59d31a4e44f08b7af67a4b67f8fd0af3befe2a |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width