<p dir="auto">Various libraries have a concept of applying partial updates to an object. For example:</p>
<p dir="auto"><a href="http://backbonejs.org/#Model-set" rel="nofollow">http://backbonejs.org/#Model-set</a><br>
<a href="http://facebook.github.io/react/docs/component-api.html#setstate" rel="nofollow">http://facebook.github.io/react/docs/component-api.html#setstate</a></p>
<p dir="auto">Take the following example (to roughly mimic Backbone)</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="interface Person {
    firstName: string
    lastName: string
    phoneNumber: string
}

var model = {
    set: function(data: Person) {}
}

model.set({
    phoneNumber: '405-555-1254'
})"><pre class="notranslate"><code class="notranslate">interface Person {
    firstName: string
    lastName: string
    phoneNumber: string
}

var model = {
    set: function(data: Person) {}
}

model.set({
    phoneNumber: '405-555-1254'
})
</code></pre></div>
<p dir="auto">In this case an error is thrown saying that properties are missing. One solution is to make all of the properties in the interface optional. However, that may not be desirable; I may actually want those properties to be required in other locations and only optional in this specific context. I would like to propose a feature where in a certain context a new type is created on-the-fly based on another type where all properties of the base type are optional. For example:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="Person? =&gt; {
    firstName?: string
    lastName?: string
    phoneNumber?: string
}"><pre class="notranslate"><code class="notranslate">Person? =&gt; {
    firstName?: string
    lastName?: string
    phoneNumber?: string
}
</code></pre></div>
<p dir="auto">and therefore the following would work:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="var model = {
    set: function(data: Person?) {}
}

model.set({
    phoneNumber: '405-555-1254'
})"><pre class="notranslate"><code class="notranslate">var model = {
    set: function(data: Person?) {}
}

model.set({
    phoneNumber: '405-555-1254'
})
</code></pre></div>
<p dir="auto">I think this would also work particularly well in conjunction with my other suggestion <a class="issue-link js-issue-link" data-error-text="Failed to load title" data-id="39751620" data-permission-text="Title is private" data-url="https://github.com/microsoft/TypeScript/issues/391" data-hovercard-type="issue" data-hovercard-url="/microsoft/TypeScript/issues/391/hovercard" href="https://github.com/microsoft/TypeScript/issues/391">#391</a></p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="set: function(data: Person?!) {}"><pre class="notranslate"><code class="notranslate">set: function(data: Person?!) {}
</code></pre></div>
<p dir="auto">Where zero or more properties from Person will match but ONLY properties from Person will match.</p>
<h1 dir="auto">Async Functions</h1>
<h1 dir="auto"><a name="user-content-1">1 Async Functions</a></h1>
<p dir="auto">This is a spec proposal for the addition of <em>Async Functions</em> (also known as <code class="notranslate">async..await</code>) as a feature of TypeScript.</p>
<h1 dir="auto"><a name="user-content-2">2 Use Cases</a></h1>
<p dir="auto"><em>Async Functions</em> 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 <code class="notranslate">Promise</code> implementation, and transposition of the function body into a compatible form to resume execution when the awaited asynchronous operation completes.</p>
<p dir="auto">This is based primarily on the Async Functions strawman proposal for ECMAScript, and C# 5.0 § 10.15 <em>Async Functions</em>.</p>
<h1 dir="auto"><a name="user-content-3">3 Introduction</a></h1>
<h2 dir="auto"><a name="user-content-3.1">3.1 Syntax</a></h2>
<p dir="auto">An <em>Async Function</em> is a <em>JavaScript Function</em>, <em>Parameterized Arrow Function</em>, <em>Method</em>, or <em>Get Accessor</em> that has been prefixed with the <code class="notranslate">async</code> modifier. This modifier informs the compiler that function body transposition is required, and that the keyword <code class="notranslate">await</code> should be treated as a unary expression instead of an identifier. An <em>Async Function</em> must provide a return type annotation that points to a compatible <code class="notranslate">Promise</code> type. Return type inference can only be used if there is a globally defined, compatible <code class="notranslate">Promise</code> type.</p>
<p dir="auto">Example:</p>
<div class="highlight highlight-source-ts notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="var p: Promise&lt;number&gt; = /* ... */;  
async function fn(): Promise&lt;number&gt; {  
  var i = await p; // suspend execution until 'p' is settled. 'i' has type &quot;number&quot;  
  return 1 + i;  
}  

var a = async (): Promise&lt;number&gt; =&gt; 1 + await p; // suspends execution.  
var a = async () =&gt; 1 + await p; // suspends execution. return type is inferred as &quot;Promise&lt;number&gt;&quot; when compiling with --target ES6  
var fe = async function(): Promise&lt;number&gt; {  
  var i = await p; // suspend execution until 'p' is settled. 'i' has type &quot;number&quot;  
  return 1 + i;  
}  

class C {  
  async m(): Promise&lt;number&gt; {  
    var i = await p; // suspend execution until 'p' is settled. 'i' has type &quot;number&quot;  
    return 1 + i;  
  }  

  async get p(): Promise&lt;number&gt; {  
    var i = await p; // suspend execution until 'p' is settled. 'i' has type &quot;number&quot;  
    return 1 + i;  
  }  
}"><pre class="notranslate"><span class="pl-k">var</span> <span class="pl-en">p</span>: <span class="pl-smi">Promise</span><span class="pl-kos">&lt;</span><span class="pl-smi">number</span><span class="pl-kos">&gt;</span> <span class="pl-c1">=</span> <span class="pl-c">/* ... */</span><span class="pl-kos">;</span>  
<span class="pl-k">async</span> <span class="pl-k">function</span> <span class="pl-en">fn</span><span class="pl-kos">(</span><span class="pl-kos">)</span>: <span class="pl-smi">Promise</span><span class="pl-kos">&lt;</span><span class="pl-smi">number</span><span class="pl-kos">&gt;</span> <span class="pl-kos">{</span>  
  <span class="pl-k">var</span> <span class="pl-s1">i</span> <span class="pl-c1">=</span> <span class="pl-k">await</span> <span class="pl-s1">p</span><span class="pl-kos">;</span> <span class="pl-c">// suspend execution until 'p' is settled. 'i' has type "number"  </span>
  <span class="pl-k">return</span> <span class="pl-c1">1</span> <span class="pl-c1">+</span> <span class="pl-s1">i</span><span class="pl-kos">;</span>  
<span class="pl-kos">}</span>  

<span class="pl-k">var</span> <span class="pl-en">a</span> <span class="pl-c1">=</span> <span class="pl-k">async</span> <span class="pl-kos">(</span><span class="pl-kos">)</span>: <span class="pl-smi">Promise</span><span class="pl-kos">&lt;</span><span class="pl-smi">number</span><span class="pl-kos">&gt;</span> <span class="pl-c1">=&gt;</span> <span class="pl-c1">1</span> <span class="pl-c1">+</span> <span class="pl-k">await</span> <span class="pl-s1">p</span><span class="pl-kos">;</span> <span class="pl-c">// suspends execution.  </span>
<span class="pl-k">var</span> <span class="pl-en">a</span> <span class="pl-c1">=</span> <span class="pl-k">async</span> <span class="pl-kos">(</span><span class="pl-kos">)</span> <span class="pl-c1">=&gt;</span> <span class="pl-c1">1</span> <span class="pl-c1">+</span> <span class="pl-k">await</span> <span class="pl-s1">p</span><span class="pl-kos">;</span> <span class="pl-c">// suspends execution. return type is inferred as "Promise&lt;number&gt;" when compiling with --target ES6  </span>
<span class="pl-k">var</span> <span class="pl-en">fe</span> <span class="pl-c1">=</span> <span class="pl-k">async</span> <span class="pl-k">function</span><span class="pl-kos">(</span><span class="pl-kos">)</span>: <span class="pl-smi">Promise</span><span class="pl-kos">&lt;</span><span class="pl-smi">number</span><span class="pl-kos">&gt;</span> <span class="pl-kos">{</span>  
  <span class="pl-k">var</span> <span class="pl-s1">i</span> <span class="pl-c1">=</span> <span class="pl-k">await</span> <span class="pl-s1">p</span><span class="pl-kos">;</span> <span class="pl-c">// suspend execution until 'p' is settled. 'i' has type "number"  </span>
  <span class="pl-k">return</span> <span class="pl-c1">1</span> <span class="pl-c1">+</span> <span class="pl-s1">i</span><span class="pl-kos">;</span>  
<span class="pl-kos">}</span>  

<span class="pl-k">class</span> <span class="pl-smi">C</span> <span class="pl-kos">{</span>  
  <span class="pl-k">async</span> <span class="pl-en">m</span><span class="pl-kos">(</span><span class="pl-kos">)</span>: <span class="pl-smi">Promise</span><span class="pl-kos">&lt;</span><span class="pl-smi">number</span><span class="pl-kos">&gt;</span> <span class="pl-kos">{</span>  
    <span class="pl-k">var</span> <span class="pl-s1">i</span> <span class="pl-c1">=</span> <span class="pl-k">await</span> <span class="pl-s1">p</span><span class="pl-kos">;</span> <span class="pl-c">// suspend execution until 'p' is settled. 'i' has type "number"  </span>
    <span class="pl-k">return</span> <span class="pl-c1">1</span> <span class="pl-c1">+</span> <span class="pl-s1">i</span><span class="pl-kos">;</span>  
  <span class="pl-kos">}</span>  

  <span class="pl-k">async</span> <span class="pl-k">get</span> <span class="pl-en">p</span><span class="pl-kos">(</span><span class="pl-kos">)</span>: <span class="pl-smi">Promise</span><span class="pl-kos">&lt;</span><span class="pl-smi">number</span><span class="pl-kos">&gt;</span> <span class="pl-kos">{</span>  
    <span class="pl-k">var</span> <span class="pl-s1">i</span> <span class="pl-c1">=</span> <span class="pl-k">await</span> <span class="pl-s1">p</span><span class="pl-kos">;</span> <span class="pl-c">// suspend execution until 'p' is settled. 'i' has type "number"  </span>
    <span class="pl-k">return</span> <span class="pl-c1">1</span> <span class="pl-c1">+</span> <span class="pl-s1">i</span><span class="pl-kos">;</span>  
  <span class="pl-kos">}</span>  
<span class="pl-kos">}</span></pre></div>
<h2 dir="auto"><a name="user-content-3.2">3.2 Transformations</a></h2>
<p dir="auto">To support this feature, the compiler needs to make certain transformations to the function body of an <em>Async Function</em>. The type of transformations performed depends on whether the current compilation target is ES6, or ES5/ES3.</p>
<h3 dir="auto"><a name="user-content-3.2.1">3.2.1 ES6 Transformations</a></h3>
<p dir="auto">During compilation of an <em>Async Function</em> when targeting ES6, the following transformations are applied:</p>
<ul dir="auto">
<li>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 <em>Async Function</em>.</li>
<li>The original function body is enclosed in a <em>Generator Function</em>.</li>
<li>Any <code class="notranslate">await</code> expressions inside the original function body are transformed into a compatible <code class="notranslate">yield</code> expression.
<ul dir="auto">
<li>As the precedence of <code class="notranslate">yield</code> is much lower than <code class="notranslate">await</code>, it may be necessary to enclose the <code class="notranslate">yield</code> expression in parenthesis if it is contained in the left-hand-side of a binary expression.</li>
</ul>
</li>
<li>The <em>Generator Function</em> is executed and the resulting generator is passed as an argument to the <code class="notranslate">__awaiter</code> helper function.</li>
<li>The result of the <code class="notranslate">__awaiter</code> helper function is passed as an argument to the promise resolve callback.</li>
</ul>
<p dir="auto">Example:</p>
<div class="highlight highlight-source-ts notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="// async.ts  
var p0: Promise&lt;number&gt; = /* ... */;  
var p1: Promise&lt;number&gt; = /* ... */;  
async function fn() {  
  var i = await p0;  
  return await p1 + i;  
}  

// async.js  
var __awaiter = /* ... */;  

function fn() {  
  return new Promise(function (_resolve) {  
    _resolve(__awaiter(function* () {  
      var i = yield p0;  
      return (yield p1) + i;  
    }()));  
  });  
}"><pre class="notranslate"><span class="pl-c">// async.ts  </span>
<span class="pl-k">var</span> <span class="pl-s1">p0</span>: <span class="pl-smi">Promise</span><span class="pl-kos">&lt;</span><span class="pl-smi">number</span><span class="pl-kos">&gt;</span> <span class="pl-c1">=</span> <span class="pl-c">/* ... */</span><span class="pl-kos">;</span>  
<span class="pl-k">var</span> <span class="pl-s1">p1</span>: <span class="pl-smi">Promise</span><span class="pl-kos">&lt;</span><span class="pl-smi">number</span><span class="pl-kos">&gt;</span> <span class="pl-c1">=</span> <span class="pl-c">/* ... */</span><span class="pl-kos">;</span>  
<span class="pl-k">async</span> <span class="pl-k">function</span> <span class="pl-en">fn</span><span class="pl-kos">(</span><span class="pl-kos">)</span> <span class="pl-kos">{</span>  
  <span class="pl-k">var</span> <span class="pl-s1">i</span> <span class="pl-c1">=</span> <span class="pl-k">await</span> <span class="pl-s1">p0</span><span class="pl-kos">;</span>  
  <span class="pl-k">return</span> <span class="pl-k">await</span> <span class="pl-s1">p1</span> <span class="pl-c1">+</span> <span class="pl-s1">i</span><span class="pl-kos">;</span>  
<span class="pl-kos">}</span>  

<span class="pl-c">// async.js  </span>
<span class="pl-k">var</span> <span class="pl-en">__awaiter</span> <span class="pl-c1">=</span> <span class="pl-c">/* ... */</span><span class="pl-kos">;</span>  

<span class="pl-k">function</span> <span class="pl-en">fn</span><span class="pl-kos">(</span><span class="pl-kos">)</span> <span class="pl-kos">{</span>  
  <span class="pl-k">return</span> <span class="pl-k">new</span> <span class="pl-smi">Promise</span><span class="pl-kos">(</span><span class="pl-k">function</span> <span class="pl-kos">(</span><span class="pl-s1">_resolve</span><span class="pl-kos">)</span> <span class="pl-kos">{</span>  
    <span class="pl-en">_resolve</span><span class="pl-kos">(</span><span class="pl-en">__awaiter</span><span class="pl-kos">(</span><span class="pl-k">function</span><span class="pl-c1">*</span> <span class="pl-kos">(</span><span class="pl-kos">)</span> <span class="pl-kos">{</span>  
      <span class="pl-k">var</span> <span class="pl-s1">i</span> <span class="pl-c1">=</span> <span class="pl-k">yield</span> <span class="pl-s1">p0</span><span class="pl-kos">;</span>  
      <span class="pl-k">return</span> <span class="pl-kos">(</span><span class="pl-k">yield</span> <span class="pl-s1">p1</span><span class="pl-kos">)</span> <span class="pl-c1">+</span> <span class="pl-s1">i</span><span class="pl-kos">;</span>  
    <span class="pl-kos">}</span><span class="pl-kos">(</span><span class="pl-kos">)</span><span class="pl-kos">)</span><span class="pl-kos">)</span><span class="pl-kos">;</span>  
  <span class="pl-kos">}</span><span class="pl-kos">)</span><span class="pl-kos">;</span>  
<span class="pl-kos">}</span></pre></div>
<h3 dir="auto"><a name="user-content-3.2.2">3.2.2 ES5/ES3 Transformations</a></h3>
<p dir="auto">As ES5 and earlier do not support <em>Generator Functions</em>, a more complex transformation is required. To support this transformation, <code class="notranslate">__generator</code> helper function will be also emitted along with the <code class="notranslate">__awaiter</code> helper function, and a much more comprehensive set of transformations would be applied:</p>
<ul dir="auto">
<li>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 <em>Async Function</em>.</li>
<li>The original function body is enclosed in a function expression with a single parameter that is passed as an argument to the <code class="notranslate">__generator</code> helper function.</li>
<li>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.</li>
<li>The original function body is rewritten into a series of case clauses in a switch statement.</li>
<li>Each statement in the function body that contains an <code class="notranslate">await</code> expression is rewritten into flattened set of instructions that are interpreted by the <code class="notranslate">__generator</code> helper function.</li>
<li>Temporary locals are generated to hold onto the values for partially-applied expressions to preserve side effects expected in the original source.</li>
<li>Logical binary expressions that contain an <code class="notranslate">await</code> expression are rewritten to preserve shortcutting.</li>
<li>Assignment expressions that contain an <code class="notranslate">await</code> are rewritten to store portions left-hand side of the assignment in temporary locals to preserve side effects.</li>
<li>Call expressions that contain an <code class="notranslate">await</code> in the argument list are rewritten to store the callee and this argument, and to instead call the <code class="notranslate">call</code> method of the callee.</li>
<li>New expressions that contain an <code class="notranslate">await</code> in the argument list are rewritten to preserve side effects.</li>
<li>Array literal expressions that contain an <code class="notranslate">await</code> in the element list are rewritten to preserve side effects.</li>
<li>Object literal expressions that contain an <code class="notranslate">await</code> in the element list are rewritten to preserve side effects.</li>
<li>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 <code class="notranslate">__generator</code> helper function.</li>
<li>The variable of a <code class="notranslate">catch</code> 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.</li>
<li><code class="notranslate">break</code> and <code class="notranslate">continue</code> statements whose target is a statement that contains an <code class="notranslate">await</code> expression are rewritten to return an instruction interpreted by the <code class="notranslate">__generator</code> helper function.</li>
<li><code class="notranslate">return</code> statements are rewritten to return an instruction interpreted by the <code class="notranslate">__generator</code> helper function.</li>
<li><code class="notranslate">for..in</code> statements that contain an <code class="notranslate">await</code> 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 <code class="notranslate">await</code> expression.</li>
<li>Labeled statements that contain an <code class="notranslate">await</code> expression are rewritten.</li>
<li><code class="notranslate">await</code> expressions are rewritten to return an instruction interpreted by the  <code class="notranslate">__generator</code> helper function.</li>
</ul>
<p dir="auto">Example:</p>
<div class="highlight highlight-source-ts notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="// async.ts  
var p0: Promise&lt;number&gt; = /* ... */;  
var p1: Promise&lt;number&gt; = /* ... */;  
async function fn() {  
    var i = await p0;  
    return await p1 + i;  
}  

// async.js  
var __awaiter = /* ... */;  
var __generator = /* ... */;  

function fn() {  
    var i;  
    return new Promise(function (_resolve) {  
        resolve(__awaiter(__generator(function (_state) {  
            switch (_state.label) {  
                case 0: return [3 /*yield*/, p0];  
                case 1:  
                    i = _state.sent;  
                    return [3 /*yield*/, p1];  
                case 2:  
                    return [2 /*return*/, _state.sent + i];  
            }  
        }));  
    });  
}"><pre class="notranslate"><span class="pl-c">// async.ts  </span>
<span class="pl-k">var</span> <span class="pl-s1">p0</span>: <span class="pl-smi">Promise</span><span class="pl-kos">&lt;</span><span class="pl-smi">number</span><span class="pl-kos">&gt;</span> <span class="pl-c1">=</span> <span class="pl-c">/* ... */</span><span class="pl-kos">;</span>  
<span class="pl-k">var</span> <span class="pl-s1">p1</span>: <span class="pl-smi">Promise</span><span class="pl-kos">&lt;</span><span class="pl-smi">number</span><span class="pl-kos">&gt;</span> <span class="pl-c1">=</span> <span class="pl-c">/* ... */</span><span class="pl-kos">;</span>  
<span class="pl-k">async</span> <span class="pl-k">function</span> <span class="pl-en">fn</span><span class="pl-kos">(</span><span class="pl-kos">)</span> <span class="pl-kos">{</span>  
    <span class="pl-k">var</span> <span class="pl-s1">i</span> <span class="pl-c1">=</span> <span class="pl-k">await</span> <span class="pl-s1">p0</span><span class="pl-kos">;</span>  
    <span class="pl-k">return</span> <span class="pl-k">await</span> <span class="pl-s1">p1</span> <span class="pl-c1">+</span> <span class="pl-s1">i</span><span class="pl-kos">;</span>  
<span class="pl-kos">}</span>  

<span class="pl-c">// async.js  </span>
<span class="pl-k">var</span> <span class="pl-s1">__awaiter</span> <span class="pl-c1">=</span> <span class="pl-c">/* ... */</span><span class="pl-kos">;</span>  
<span class="pl-k">var</span> <span class="pl-en">__generator</span> <span class="pl-c1">=</span> <span class="pl-c">/* ... */</span><span class="pl-kos">;</span>  

<span class="pl-k">function</span> <span class="pl-en">fn</span><span class="pl-kos">(</span><span class="pl-kos">)</span> <span class="pl-kos">{</span>  
    <span class="pl-k">var</span> <span class="pl-s1">i</span><span class="pl-kos">;</span>  
    <span class="pl-k">return</span> <span class="pl-k">new</span> <span class="pl-smi">Promise</span><span class="pl-kos">(</span><span class="pl-k">function</span> <span class="pl-kos">(</span><span class="pl-s1">_resolve</span><span class="pl-kos">)</span> <span class="pl-kos">{</span>  
        <span class="pl-en">resolve</span><span class="pl-kos">(</span><span class="pl-en">__awaiter</span><span class="pl-kos">(</span><span class="pl-en">__generator</span><span class="pl-kos">(</span><span class="pl-k">function</span> <span class="pl-kos">(</span><span class="pl-s1">_state</span><span class="pl-kos">)</span> <span class="pl-kos">{</span>  
            <span class="pl-k">switch</span> <span class="pl-kos">(</span><span class="pl-s1">_state</span><span class="pl-kos">.</span><span class="pl-c1">label</span><span class="pl-kos">)</span> <span class="pl-kos">{</span>  
                <span class="pl-k">case</span> <span class="pl-c1">0</span>: <span class="pl-k">return</span> <span class="pl-kos">[</span><span class="pl-c1">3</span> <span class="pl-c">/*yield*/</span><span class="pl-kos">,</span> <span class="pl-s1">p0</span><span class="pl-kos">]</span><span class="pl-kos">;</span>  
                <span class="pl-k">case</span> <span class="pl-c1">1</span>:  
                    <span class="pl-s1">i</span> <span class="pl-c1">=</span> <span class="pl-s1">_state</span><span class="pl-kos">.</span><span class="pl-c1">sent</span><span class="pl-kos">;</span>  
                    <span class="pl-k">return</span> <span class="pl-kos">[</span><span class="pl-c1">3</span> <span class="pl-c">/*yield*/</span><span class="pl-kos">,</span> <span class="pl-s1">p1</span><span class="pl-kos">]</span><span class="pl-kos">;</span>  
                <span class="pl-k">case</span> <span class="pl-c1">2</span>:  
                    <span class="pl-k">return</span> <span class="pl-kos">[</span><span class="pl-c1">2</span> <span class="pl-c">/*return*/</span><span class="pl-kos">,</span> <span class="pl-s1">_state</span><span class="pl-kos">.</span><span class="pl-c1">sent</span> <span class="pl-c1">+</span> <span class="pl-s1">i</span><span class="pl-kos">]</span><span class="pl-kos">;</span>  
            <span class="pl-kos">}</span>  
        <span class="pl-kos">}</span><span class="pl-kos">)</span><span class="pl-kos">)</span><span class="pl-kos"></span><span class="pl-kos">;</span>  
    <span class="pl-kos">}</span><span class="pl-kos">)</span><span class="pl-kos">;</span>  
<span class="pl-kos">}</span></pre></div>
<p dir="auto">The following is an example of an async function that contains a <code class="notranslate">try</code> statement:</p>
<div class="highlight highlight-source-ts notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="// async.ts  
var p0: Promise&lt;number&gt; = /* ... */;  
var p1: Promise&lt;number&gt; = /* ... */;  
async function fn() {  
    try {  
        await p0;  
    }  
    catch (e) {  
        alert(e.message);  
    }  
    finally {  
        await p1;  
    }  
}  

// async.js  
var __awaiter = /* ... */;  
var __generator = /* .. */;  

function fn() {  
    var i;  
    return new Promise(function (_resolve) {  
        resolve(__awaiter(__generator(function (_state) {  
            switch (_state.label) {  
                case 0:  
                    _state.trys = [];  
                    _state.label = 1;  
                case 1:  
                    _state.trys.push([1, 3, 4, 6]);  
                    return [3 /*yield*/, p0];  
                case 2:  
                    return [5 /*break*/, 6];  
                case 3:  
                    _a = _state.error;  
                    alert(_a.message);  
                    return [5 /*break*/, 6];  
                case 4:  
                    return [3 /*yield*/, p1];  
                case 5:  
                    return [6 /*endfinally*/];  
                case 6:  
                    return [2 /*return*/];  
            }  
        }));  
    });  
    var _a;  
}"><pre class="notranslate"><span class="pl-c">// async.ts  </span>
<span class="pl-k">var</span> <span class="pl-s1">p0</span>: <span class="pl-smi">Promise</span><span class="pl-kos">&lt;</span><span class="pl-smi">number</span><span class="pl-kos">&gt;</span> <span class="pl-c1">=</span> <span class="pl-c">/* ... */</span><span class="pl-kos">;</span>  
<span class="pl-k">var</span> <span class="pl-s1">p1</span>: <span class="pl-smi">Promise</span><span class="pl-kos">&lt;</span><span class="pl-smi">number</span><span class="pl-kos">&gt;</span> <span class="pl-c1">=</span> <span class="pl-c">/* ... */</span><span class="pl-kos">;</span>  
<span class="pl-k">async</span> <span class="pl-k">function</span> <span class="pl-en">fn</span><span class="pl-kos">(</span><span class="pl-kos">)</span> <span class="pl-kos">{</span>  
    <span class="pl-k">try</span> <span class="pl-kos">{</span>  
        <span class="pl-k">await</span> <span class="pl-s1">p0</span><span class="pl-kos">;</span>  
    <span class="pl-kos">}</span>  
    <span class="pl-k">catch</span> <span class="pl-kos">(</span><span class="pl-s1">e</span><span class="pl-kos">)</span> <span class="pl-kos">{</span>  
        <span class="pl-en">alert</span><span class="pl-kos">(</span><span class="pl-s1">e</span><span class="pl-kos">.</span><span class="pl-c1">message</span><span class="pl-kos">)</span><span class="pl-kos">;</span>  
    <span class="pl-kos">}</span>  
    <span class="pl-k">finally</span> <span class="pl-kos">{</span>  
        <span class="pl-k">await</span> <span class="pl-s1">p1</span><span class="pl-kos">;</span>  
    <span class="pl-kos">}</span>  
<span class="pl-kos">}</span>  

<span class="pl-c">// async.js  </span>
<span class="pl-k">var</span> <span class="pl-s1">__awaiter</span> <span class="pl-c1">=</span> <span class="pl-c">/* ... */</span><span class="pl-kos">;</span>  
<span class="pl-k">var</span> <span class="pl-en">__generator</span> <span class="pl-c1">=</span> <span class="pl-c">/* .. */</span><span class="pl-kos">;</span>  

<span class="pl-k">function</span> <span class="pl-en">fn</span><span class="pl-kos">(</span><span class="pl-kos">)</span> <span class="pl-kos">{</span>  
    <span class="pl-k">var</span> <span class="pl-s1">i</span><span class="pl-kos">;</span>  
    <span class="pl-k">return</span> <span class="pl-k">new</span> <span class="pl-smi">Promise</span><span class="pl-kos">(</span><span class="pl-k">function</span> <span class="pl-kos">(</span><span class="pl-s1">_resolve</span><span class="pl-kos">)</span> <span class="pl-kos">{</span>  
        <span class="pl-en">resolve</span><span class="pl-kos">(</span><span class="pl-en">__awaiter</span><span class="pl-kos">(</span><span class="pl-en">__generator</span><span class="pl-kos">(</span><span class="pl-k">function</span> <span class="pl-kos">(</span><span class="pl-s1">_state</span><span class="pl-kos">)</span> <span class="pl-kos">{</span>  
            <span class="pl-k">switch</span> <span class="pl-kos">(</span><span class="pl-s1">_state</span><span class="pl-kos">.</span><span class="pl-c1">label</span><span class="pl-kos">)</span> <span class="pl-kos">{</span>  
                <span class="pl-k">case</span> <span class="pl-c1">0</span>:  
                    <span class="pl-s1">_state</span><span class="pl-kos">.</span><span class="pl-c1">trys</span> <span class="pl-c1">=</span> <span class="pl-kos">[</span><span class="pl-kos">]</span><span class="pl-kos">;</span>  
                    <span class="pl-s1">_state</span><span class="pl-kos">.</span><span class="pl-c1">label</span> <span class="pl-c1">=</span> <span class="pl-c1">1</span><span class="pl-kos">;</span>  
                <span class="pl-k">case</span> <span class="pl-c1">1</span>:  
                    <span class="pl-s1">_state</span><span class="pl-kos">.</span><span class="pl-c1">trys</span><span class="pl-kos">.</span><span class="pl-en">push</span><span class="pl-kos">(</span><span class="pl-kos">[</span><span class="pl-c1">1</span><span class="pl-kos">,</span> <span class="pl-c1">3</span><span class="pl-kos">,</span> <span class="pl-c1">4</span><span class="pl-kos">,</span> <span class="pl-c1">6</span><span class="pl-kos">]</span><span class="pl-kos">)</span><span class="pl-kos">;</span>  
                    <span class="pl-k">return</span> <span class="pl-kos">[</span><span class="pl-c1">3</span> <span class="pl-c">/*yield*/</span><span class="pl-kos">,</span> <span class="pl-s1">p0</span><span class="pl-kos">]</span><span class="pl-kos">;</span>  
                <span class="pl-k">case</span> <span class="pl-c1">2</span>:  
                    <span class="pl-k">return</span> <span class="pl-kos">[</span><span class="pl-c1">5</span> <span class="pl-c">/*break*/</span><span class="pl-kos">,</span> <span class="pl-c1">6</span><span class="pl-kos">]</span><span class="pl-kos">;</span>  
                <span class="pl-k">case</span> <span class="pl-c1">3</span>:  
                    <span class="pl-s1">_a</span> <span class="pl-c1">=</span> <span class="pl-s1">_state</span><span class="pl-kos">.</span><span class="pl-c1">error</span><span class="pl-kos">;</span>  
                    <span class="pl-en">alert</span><span class="pl-kos">(</span><span class="pl-s1">_a</span><span class="pl-kos">.</span><span class="pl-c1">message</span><span class="pl-kos">)</span><span class="pl-kos">;</span>  
                    <span class="pl-k">return</span> <span class="pl-kos">[</span><span class="pl-c1">5</span> <span class="pl-c">/*break*/</span><span class="pl-kos">,</span> <span class="pl-c1">6</span><span class="pl-kos">]</span><span class="pl-kos">;</span>  
                <span class="pl-k">case</span> <span class="pl-c1">4</span>:  
                    <span class="pl-k">return</span> <span class="pl-kos">[</span><span class="pl-c1">3</span> <span class="pl-c">/*yield*/</span><span class="pl-kos">,</span> <span class="pl-s1">p1</span><span class="pl-kos">]</span><span class="pl-kos">;</span>  
                <span class="pl-k">case</span> <span class="pl-c1">5</span>:  
                    <span class="pl-k">return</span> <span class="pl-kos">[</span><span class="pl-c1">6</span> <span class="pl-c">/*endfinally*/</span><span class="pl-kos">]</span><span class="pl-kos">;</span>  
                <span class="pl-k">case</span> <span class="pl-c1">6</span>:  
                    <span class="pl-k">return</span> <span class="pl-kos">[</span><span class="pl-c1">2</span> <span class="pl-c">/*return*/</span><span class="pl-kos">]</span><span class="pl-kos">;</span>  
            <span class="pl-kos">}</span>  
        <span class="pl-kos">}</span><span class="pl-kos">)</span><span class="pl-kos">)</span><span class="pl-kos"></span><span class="pl-kos">;</span>  
    <span class="pl-kos">}</span><span class="pl-kos">)</span><span class="pl-kos">;</span>  
    <span class="pl-k">var</span> <span class="pl-s1">_a</span><span class="pl-kos">;</span>  
<span class="pl-kos">}</span></pre></div>
<p dir="auto">As a result of these transformations, the JavaScript output for an <em>Async Function</em> can look quite different than the original source. When debugging the JavaScript output for an <em>Async Function</em> it would be advisable to use a Source Map generated using the <code class="notranslate">--sourceMap</code> option for the compiler.</p>
<h1 dir="auto"><a name="user-content-4">4 Promise</a></h1>
<p dir="auto"><em>Async Functions</em> 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):</p>
<div class="highlight highlight-source-ts notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="interface IPromiseConstructor&lt;T&gt; {  
    new (init: (resolve: (value: T | IPromise&lt;T&gt;) =&gt; void, reject: (reason: any) =&gt; void) =&gt; void): IPromise&lt;T&gt;;  
}  

interface IPromise&lt;T&gt; {  
    then&lt;TResult&gt;(onfulfilled: (value: T) =&gt; TResult | IPromise&lt;TResult&gt;, onrejected: (reason: any) =&gt; TResult | IPromise&lt;TResult&gt;): IPromise&lt;TResult&gt;;  
}"><pre class="notranslate"><span class="pl-k">interface</span> <span class="pl-smi">IPromiseConstructor</span><span class="pl-c1">&lt;</span><span class="pl-smi">T</span><span class="pl-c1">&gt;</span> <span class="pl-kos">{</span>  
    <span class="pl-k">new</span> <span class="pl-kos">(</span><span class="pl-s1">init</span>: <span class="pl-kos">(</span><span class="pl-s1">resolve</span>: <span class="pl-kos">(</span><span class="pl-s1">value</span>: <span class="pl-smi">T</span> <span class="pl-c1">|</span> <span class="pl-smi">IPromise</span><span class="pl-kos">&lt;</span><span class="pl-smi">T</span><span class="pl-kos">&gt;</span><span class="pl-kos">)</span> <span class="pl-c1">=&gt;</span> <span class="pl-smi"><span class="pl-k">void</span></span><span class="pl-kos">,</span> <span class="pl-s1">reject</span>: <span class="pl-kos">(</span><span class="pl-s1">reason</span>: <span class="pl-smi">any</span><span class="pl-kos">)</span> <span class="pl-c1">=&gt;</span> <span class="pl-smi"><span class="pl-k">void</span></span><span class="pl-kos">)</span> <span class="pl-c1">=&gt;</span> <span class="pl-smi"><span class="pl-k">void</span></span><span class="pl-kos">)</span>: <span class="pl-smi">IPromise</span><span class="pl-kos">&lt;</span><span class="pl-smi">T</span><span class="pl-kos">&gt;</span><span class="pl-kos">;</span>  
<span class="pl-kos">}</span>  

<span class="pl-k">interface</span> <span class="pl-smi">IPromise</span><span class="pl-c1">&lt;</span><span class="pl-smi">T</span><span class="pl-c1">&gt;</span> <span class="pl-kos">{</span>  
    <span class="pl-c1">then</span><span class="pl-c1">&lt;</span><span class="pl-smi">TResult</span><span class="pl-c1">&gt;</span><span class="pl-kos">(</span><span class="pl-s1">onfulfilled</span>: <span class="pl-kos">(</span><span class="pl-s1">value</span>: <span class="pl-smi">T</span><span class="pl-kos">)</span> <span class="pl-c1">=&gt;</span> <span class="pl-smi">TResult</span> <span class="pl-c1">|</span> <span class="pl-smi">IPromise</span><span class="pl-kos">&lt;</span><span class="pl-smi">TResult</span><span class="pl-kos">&gt;</span><span class="pl-kos">,</span> <span class="pl-s1">onrejected</span>: <span class="pl-kos">(</span><span class="pl-s1">reason</span>: <span class="pl-smi">any</span><span class="pl-kos">)</span> <span class="pl-c1">=&gt;</span> <span class="pl-smi">TResult</span> <span class="pl-c1">|</span> <span class="pl-smi">IPromise</span><span class="pl-kos">&lt;</span><span class="pl-smi">TResult</span><span class="pl-kos">&gt;</span><span class="pl-kos">)</span>: <span class="pl-smi">IPromise</span><span class="pl-kos">&lt;</span><span class="pl-smi">TResult</span><span class="pl-kos">&gt;</span><span class="pl-kos">;</span>  
<span class="pl-kos">}</span></pre></div>
<p dir="auto">The following libraries contain compatible Promise implementations:</p>
<ul dir="auto">
<li>ES6 - <a href="http://people.mozilla.org/~jorendorff/es6-draft.html#sec-promise-objects" rel="nofollow">http://people.mozilla.org/~jorendorff/es6-draft.html#sec-promise-objects</a></li>
<li>WinJS - <a href="http://msdn.microsoft.com/en-us/library/windows/apps/br211867.aspx" rel="nofollow">http://msdn.microsoft.com/en-us/library/windows/apps/br211867.aspx</a></li>
<li>Q - <a href="https://github.com/kriskowal/q">https://github.com/kriskowal/q</a></li>
<li>RSVP - <a href="https://github.com/tildeio/rsvp.js/">https://github.com/tildeio/rsvp.js/</a></li>
<li>Bluebird - <a href="https://github.com/petkaantonov/bluebird">https://github.com/petkaantonov/bluebird</a></li>
<li>es6-promise - <a href="https://github.com/jakearchibald/es6-promise">https://github.com/jakearchibald/es6-promise</a></li>
<li>lie - <a href="https://github.com/calvinmetcalf/lie">https://github.com/calvinmetcalf/lie</a></li>
<li>when - <a href="https://github.com/cujojs/when">https://github.com/cujojs/when</a></li>
<li>asyncjs - <a href="https://github.com/rbuckton/asyncjs">https://github.com/rbuckton/asyncjs</a></li>
</ul>
<h1 dir="auto"><a name="user-content-a">A Grammar</a></h1>
<h2 dir="auto"><a name="user-content-a.1">A.1 Types</a></h2>
<p dir="auto">  <em>CallSignature</em><sub> [Await,AsyncParameter] </sub><em>:<br>
   TypeParameters</em><sub> opt</sub><em> </em><code class="notranslate">(</code><em> ParameterList</em><sub> [?Await,?AsyncParameter]opt</sub><em> </em><code class="notranslate">)</code><em> TypeAnnotation</em><sub> opt</sub>*</p>
<p dir="auto">  ParameterList_<sub> [Await,AsyncParameter]</sub> *:_<br>
   <em>RequiredParameterList</em><sub> [?Await]</sub><br>
   <em>OptionalParameterList</em><sub> [?Await,?AsyncParameter]</sub><br>
   <em>RestParameter</em><sub> [?Await]</sub><br>
   <em>RequiredParameterList</em><sub> [?Await]</sub> <code class="notranslate">,</code> <em>OptionalParameterList</em><sub> [?Await,?AsyncParameter]</sub><br>
   <em>RequiredParameterList</em><sub> [?Await]</sub> <code class="notranslate">,</code> <em>RestParameter</em><sub> [?Await]</sub><br>
   <em>OptionalParameterList</em><sub> [?Await,?AsyncParameter]</sub> <code class="notranslate">,</code> <em>RestParameter</em><sub> [?Await]</sub><br>
   <em>RequiredParameterList</em><sub> [?Await]</sub> <code class="notranslate">,</code> <em>OptionalParameterList</em><sub> [?Await,?AsyncParameter]</sub> <code class="notranslate">,</code> <em>RestParameter</em><sub> [?Await]</sub></p>
<p dir="auto">  <em>RequiredParameterList</em><sub> [Await] </sub><em>:</em><br>
   <em>RequiredParameter</em><sub> [?Await]</sub><br>
   <em>RequiredParameterList</em><sub> [?Await]</sub> <code class="notranslate">,</code> <em>RequiredParameter</em><sub> [?Await]</sub></p>
<p dir="auto">  <em>RequiredParameter</em><sub> [Await] </sub><em>:</em><br>
   <em>AccessibilityModifier</em><sub> opt</sub> <em>BindingIdentifier</em><sub> [?Await]</sub> <em>TypeAnnotation</em><sub> opt</sub><br>
   <em>Identifier</em> <code class="notranslate">:</code> <em>StringLiteral</em></p>
<p dir="auto">  <em>OptionalParameterList</em><sub> [Await,AsyncParameter] </sub><em>:</em><br>
   <em>OptionalParameter</em><sub> [?Await,?AsyncParameter]</sub><br>
   <em>OptionalParameterList</em><sub> [?Await,?AsyncParameter]</sub> <code class="notranslate">,</code> <em>OptionalParameter</em><sub> [?Await,?AsyncParameter]</sub></p>
<p dir="auto">  <em>OptionalParameter</em><sub> [Await,AsyncParameter]</sub><em>:</em><br>
   [+AsyncParameter] <em>AccessibilityModifier</em><sub> opt</sub> <em>BindingIdentifier</em><sub> [Await]</sub> <code class="notranslate">?</code> <em>TypeAnnotation</em><sub> opt</sub><br>
   [+AsyncParameter] <em>AccessibilityModifier</em><sub> opt</sub> <em>BindingIdentifier</em><sub> [Await]</sub> <em>TypeAnnotation</em><sub> opt</sub> <em>Initialiser</em><sub> [In]</sub><br>
   [+AsyncParameter] <em>BindingIdentifier</em><sub> [Await]</sub> <code class="notranslate">?</code> <code class="notranslate">:</code> <em>StringLiteral</em><br>
   [~AsyncParameter] <em>AccessibilityModifier</em><sub> opt</sub> <em>BindingIdentifier</em><sub> [?Await]</sub> <code class="notranslate">?</code> <em>TypeAnnotation</em><sub> opt</sub><br>
   [~AsyncParameter] <em>AccessibilityModifier</em><sub> opt</sub> <em>BindingIdentifier</em><sub> [?Await]</sub> <em>TypeAnnotation</em><sub> opt</sub> <em>Initialiser</em><sub> [In,?Await]</sub><br>
   [~AsyncParameter] <em>BindingIdentifier</em><sub> [?Await]</sub> <code class="notranslate">?</code> <code class="notranslate">:</code> <em>StringLiteral</em></p>
<p dir="auto">  <em>RestParameter</em><sub> [Await]</sub><em>:</em><br>
   <code class="notranslate">...</code> <em>BindingIdentifier</em><sub> [?Await]</sub> <em>TypeAnnotation</em><sub> opt</sub></p>
<h2 dir="auto"><a name="user-content-a.2">A.2 Expressions</a></h2>
<p dir="auto">  <em>BindingIdentifier</em><sub> [Await]</sub> : ( Modified )<br>
   <em>Identifier</em> but not <code class="notranslate">await</code><br>
   [~Await] <code class="notranslate">await</code></p>
<p dir="auto">  <em>PropertyAssignment</em><sub> [Await]</sub> :<br>
   <em>PropertyName</em> <code class="notranslate">:</code> <em>AssignmentExpression</em><sub> [?Await]</sub><br>
   <em>PropertyName</em> <em>CallSignature</em> <code class="notranslate">{</code> <em>FunctionBody</em> <code class="notranslate">}</code><br>
   <em>GetAccessor</em><br>
   <em>SetAccessor</em><br>
   <code class="notranslate">async</code> [no <em>LineTerminator</em> here] <em>PropertyName</em> <em>CallSignature</em><sub> [Await,AsyncParameter]</sub> <code class="notranslate">{</code> <em>FunctionBody</em><sub> [Await]</sub> <code class="notranslate">}</code></p>
<p dir="auto">  <em>GetAccessor</em> :<br>
   <code class="notranslate">get</code> <em>PropertyName</em> <code class="notranslate">(</code> <code class="notranslate">)</code> <em>TypeAnnotation<sub>opt</sub></em> <code class="notranslate">{</code> <em>FunctionBody</em> <code class="notranslate">}</code><br>
   <code class="notranslate">async</code> [no <em>LineTerminator</em> here] <code class="notranslate">get</code> <em>PropertyName</em> <code class="notranslate">(</code> <code class="notranslate">)</code> <em>TypeAnnotation</em><sub> opt</sub> <code class="notranslate">{</code> <em>FunctionBody</em><sub> [Await]</sub> <code class="notranslate">}</code></p>
<p dir="auto">  <em>FunctionExpression</em><sub> [Await]</sub> : ( Modified )<br>
   <code class="notranslate">function</code> <em>BindingIdentifier</em><sub> [?Await]opt</sub> <em>CallSignature</em> <code class="notranslate">{</code> <em>FunctionBody</em> <code class="notranslate">}</code><br>
   <code class="notranslate">async</code> [no <em>LineTerminator</em> here] <code class="notranslate">function</code> <em>BindingIdentifier</em><sub> [?Await]opt</sub> <em>CallSignature</em><sub> [Await,AsyncParameter]</sub> <code class="notranslate">{</code> <em>FunctionBody</em><sub> [Await]</sub> <code class="notranslate">}</code></p>
<p dir="auto">  <em>AssignmentExpression</em><sub> [Await]</sub> : ( Modified )<br>
   ...<br>
   <em>ArrowFunctionExpression</em><sub> [?Await]</sub></p>
<p dir="auto">  <em>ArrowFunctionExpression</em><sub> [Await]</sub> :<br>
   <em>ArrowFormalParameters</em> <code class="notranslate">=&gt;</code> <em>Block</em><sub> [?Await]</sub><br>
   <em>ArrowFormalParameters</em> <code class="notranslate">=&gt;</code> <em>AssignmentExpression</em><sub> [?Await]</sub><br>
   <code class="notranslate">async</code> [no <em>LineTerminator</em> here] <em>CallSignature</em><sub> [Await,AsyncParameter]</sub> <code class="notranslate">=&gt;</code> <em>Block</em><sub> [Await]</sub><br>
   <code class="notranslate">async</code> [no <em>LineTerminator</em> here] <em>CallSignature</em><sub> [Await,AsyncParameter]</sub> <code class="notranslate">=&gt;</code> <em>AssignmentExpression</em><sub> [Await]</sub></p>
<p dir="auto">  <em>ArrowFormalParameters</em><sub> [Await]</sub> :<br>
   <em>CallSignature</em><br>
   <em>BindingIdentifier</em><sub> [?Await]</sub></p>
<p dir="auto">  <em>UnaryExpression</em><sub> [Await]</sub> : ( Modified )<br>
   ...<br>
   <code class="notranslate">&lt;</code> <em>Type</em> <code class="notranslate">&gt;</code> <em>UnaryExpression</em><sub> [?Await]</sub><br>
   [+Await] <code class="notranslate">await</code> <em>UnaryExpression</em><sub> [Await]</sub></p>
<h2 dir="auto"><a name="user-content-a.3">A.3 Functions</a></h2>
<p dir="auto">  <em>FunctionDeclaration</em><sub> [Await]</sub> : ( Modified )<br>
   <em>FunctionOverloads</em><sub> [?Await]opt</sub> <em>FunctionImplementation</em><sub> [?Await]</sub></p>
<p dir="auto">  <em>FunctionOverloads</em><sub> [Await]</sub> :<br>
   <em>FunctionOverloads</em><sub> [?Await]opt</sub> <em>FunctionOverload</em><sub> [?Await]</sub></p>
<p dir="auto">  <em>FunctionOverload</em><sub> [Await]</sub> :<br>
   <code class="notranslate">function</code> <em>BindingIdentifier</em><sub> [?Await]</sub> <em>CallSignature</em> <code class="notranslate">;</code></p>
<p dir="auto">  FunctionImplementation<sub> [Await]</sub> :<br>
   <code class="notranslate">function</code> <em>BindingIdentifier</em><sub> [?Await]</sub> <em>CallSignature</em> <code class="notranslate">{</code> <em>FunctionBody</em> <code class="notranslate">}</code><br>
   <code class="notranslate">async</code> [no <em>LineTerminator</em> here] <code class="notranslate">function</code> <em>BindingIdentifier</em><sub> [?Await]</sub> <em>CallSignature</em><sub> [Await,AsyncParameter]</sub> <code class="notranslate">{</code> <em>FunctionBody</em><sub> [Await]</sub> <code class="notranslate">}</code></p>
<h2 dir="auto"><a name="user-content-a.4">A.4 Classes</a></h2>
<p dir="auto">  <em>MemberFunctionImplementation</em>:<br>
   <em>AccessibilityModifier</em><sub>opt</sub> <code class="notranslate">static</code><sub> opt</sub> <em>PropertyName</em> <em>CallSignature</em> <code class="notranslate">{</code> <em>FunctionBody</em> <code class="notranslate">}</code><br>
   <em>AccessibilityModifier</em><sub>opt</sub> <code class="notranslate">static</code><sub> opt</sub> <code class="notranslate">async</code> [no <em>LineTerminator</em> here] <em>PropertyName</em> <em>CallSignature</em><sub> [Await,AsyncParameter]</sub> <code class="notranslate">{</code> <em>FunctionBody</em><sub> [Await]</sub> <code class="notranslate">}</code></p>
<h1 dir="auto"><a name="user-content-b">B Helper Functions</a></h1>
<p dir="auto">There are two helper functions that are used for <em>Async Functions</em>. The <code class="notranslate">__awaiter</code> helper function is used by both the ES6 as well as the ES5/ES3 transformations. The <code class="notranslate">__generator</code> helper function is used only by the ES5/ES3 transformation.</p>
<h2 dir="auto"><a name="user-content-b.1">B.1  <code class="notranslate">__awaiter</code>  helper function</a></h2>
<div class="highlight highlight-source-ts notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="var __awaiter = __awaiter || function (g) {  
    function n(r, t) {  
        while (true) {  
            if (r.done) return r.value;  
            if (r.value &amp;&amp; typeof (t = r.value.then) === &quot;function&quot;)  
                return t.call(r.value, function (v) { return n(g.next(v)) }, function (v) { return n(g[&quot;throw&quot;](v)) });  
            r = g.next(r.value);  
        }  
    }  
    return n(g.next());  
};"><pre class="notranslate"><span class="pl-k">var</span> <span class="pl-s1">__awaiter</span> <span class="pl-c1">=</span> <span class="pl-s1">__awaiter</span> <span class="pl-c1">||</span> <span class="pl-k">function</span> <span class="pl-kos">(</span><span class="pl-s1">g</span><span class="pl-kos">)</span> <span class="pl-kos">{</span>  
    <span class="pl-k">function</span> <span class="pl-en">n</span><span class="pl-kos">(</span><span class="pl-s1">r</span><span class="pl-kos">,</span> <span class="pl-s1">t</span><span class="pl-kos">)</span> <span class="pl-kos">{</span>  
        <span class="pl-k">while</span> <span class="pl-kos">(</span><span class="pl-c1">true</span><span class="pl-kos">)</span> <span class="pl-kos">{</span>  
            <span class="pl-k">if</span> <span class="pl-kos">(</span><span class="pl-s1">r</span><span class="pl-kos">.</span><span class="pl-c1">done</span><span class="pl-kos">)</span> <span class="pl-k">return</span> <span class="pl-s1">r</span><span class="pl-kos">.</span><span class="pl-c1">value</span><span class="pl-kos">;</span>  
            <span class="pl-k">if</span> <span class="pl-kos">(</span><span class="pl-s1">r</span><span class="pl-kos">.</span><span class="pl-c1">value</span> <span class="pl-c1">&amp;&amp;</span> <span class="pl-k">typeof</span> <span class="pl-kos">(</span><span class="pl-s1">t</span> <span class="pl-c1">=</span> <span class="pl-s1">r</span><span class="pl-kos">.</span><span class="pl-c1">value</span><span class="pl-kos">.</span><span class="pl-c1">then</span><span class="pl-kos">)</span> <span class="pl-c1">===</span> <span class="pl-s">"function"</span><span class="pl-kos">)</span>  
                <span class="pl-k">return</span> <span class="pl-s1">t</span><span class="pl-kos">.</span><span class="pl-en">call</span><span class="pl-kos">(</span><span class="pl-s1">r</span><span class="pl-kos">.</span><span class="pl-c1">value</span><span class="pl-kos">,</span> <span class="pl-k">function</span> <span class="pl-kos">(</span><span class="pl-s1">v</span><span class="pl-kos">)</span> <span class="pl-kos">{</span> <span class="pl-k">return</span> <span class="pl-en">n</span><span class="pl-kos">(</span><span class="pl-s1">g</span><span class="pl-kos">.</span><span class="pl-en">next</span><span class="pl-kos">(</span><span class="pl-s1">v</span><span class="pl-kos">)</span><span class="pl-kos">)</span> <span class="pl-kos">}</span><span class="pl-kos">,</span> <span class="pl-k">function</span> <span class="pl-kos">(</span><span class="pl-s1">v</span><span class="pl-kos">)</span> <span class="pl-kos">{</span> <span class="pl-k">return</span> <span class="pl-en">n</span><span class="pl-kos">(</span><span class="pl-s1">g</span><span class="pl-kos">[</span><span class="pl-s">"throw"</span><span class="pl-kos">]</span><span class="pl-kos">(</span><span class="pl-s1">v</span><span class="pl-kos">)</span><span class="pl-kos">)</span> <span class="pl-kos">}</span><span class="pl-kos">)</span><span class="pl-kos">;</span>  
            <span class="pl-s1">r</span> <span class="pl-c1">=</span> <span class="pl-s1">g</span><span class="pl-kos">.</span><span class="pl-en">next</span><span class="pl-kos">(</span><span class="pl-s1">r</span><span class="pl-kos">.</span><span class="pl-c1">value</span><span class="pl-kos">)</span><span class="pl-kos">;</span>  
        <span class="pl-kos">}</span>  
    <span class="pl-kos">}</span>  
    <span class="pl-k">return</span> <span class="pl-en">n</span><span class="pl-kos">(</span><span class="pl-s1">g</span><span class="pl-kos">.</span><span class="pl-en">next</span><span class="pl-kos">(</span><span class="pl-kos">)</span><span class="pl-kos">)</span><span class="pl-kos">;</span>  
<span class="pl-kos">}</span><span class="pl-kos">;</span></pre></div>
<h2 dir="auto"><a name="user-content-b.2">B.2  <code class="notranslate">__generator</code>  helper function</a></h2>
<div class="highlight highlight-source-ts notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="var __generator = __generator || function (m) {  
    var d, i = [], f, g, s = { label: 0 }, y, b;  
    function n(c) {  
        if (f) throw new TypeError(&quot;Generator is already executing.&quot;);  
        switch (d &amp;&amp; c[0]) {  
            case 0 /*next*/: return { value: void 0, done: true };  
            case 1 /*throw*/: throw c[1];  
            case 2 /*return*/: return { value: c[1], done: true };  
        }  
        while (true) {  
            f = false;  
            switch (!(g = s.trys &amp;&amp; s.trys.length &amp;&amp; s.trys[s.trys.length - 1]) &amp;&amp; c[0]) {  
                case 1 /*throw*/: i.length = 0; d = true; throw c[1];  
                case 2 /*return*/: i.length = 0; d = true; return { value: c[1], done: true };  
            }  
            try {  
                if (y) {  
                    f = true;  
                    if (typeof (b = y[c[0]]) === &quot;function&quot;) {  
                        b = b.call(y, c[1]);  
                        if (!b.done) return b;  
                        c[0] = 0 /*next*/, c[1] = b.value;  
                    }  
                    y = undefined;  
                    f = false;  
                }  
                switch (c[0]) {  
                    case 0 /*next*/: s.sent = c[1]; break;  
                    case 3 /*yield*/: s.label++; return { value: c[1], done: false };  
                    case 4 /*yield**/: s.label++; y = c[1]; c[0] = 0 /*next*/; c[1] = void 0; continue;  
                    case 6 /*endfinally*/: c = i.pop(); continue;  
                    default:  
                        if (c[0] === 1 /*throw*/ &amp;&amp; s.label &lt; g[1]) { s.error = c[1]; s.label = g[1]; break; }  
                        if (c[0] === 5 /*break*/ &amp;&amp; (!g || (c[1] &gt;= g[0] &amp;&amp; c[1] &lt; g[3]))) { s.label = c[1]; break; }  
                        s.trys.pop();  
                        if (g[2]) { i.push(c); s.label = g[2]; break; }  
                        continue;  
                }  
                f = true;  
                c = m(s);  
            } catch (e) {  
                y = void 0;  
                c[0] = 1 /*throw*/, c[1] = e;  
            }  
        }  
    }  
    return {  
        next: function (v) { return n([0 /*next*/, v]); },  
        1 /*throw*/: function (v) { return n([1 /*throw*/, v]); },  
        2 /*return*/: function (v) { return n([2 /*return*/, v]); },  
    };  
};"><pre class="notranslate"><span class="pl-k">var</span> <span class="pl-s1">__generator</span> <span class="pl-c1">=</span> <span class="pl-s1">__generator</span> <span class="pl-c1">||</span> <span class="pl-k">function</span> <span class="pl-kos">(</span><span class="pl-s1">m</span><span class="pl-kos">)</span> <span class="pl-kos">{</span>  
    <span class="pl-k">var</span> <span class="pl-s1">d</span><span class="pl-kos">,</span> <span class="pl-s1">i</span> <span class="pl-c1">=</span> <span class="pl-kos">[</span><span class="pl-kos">]</span><span class="pl-kos">,</span> <span class="pl-s1">f</span><span class="pl-kos">,</span> <span class="pl-s1">g</span><span class="pl-kos">,</span> <span class="pl-s1">s</span> <span class="pl-c1">=</span> <span class="pl-kos">{</span> <span class="pl-c1">label</span>: <span class="pl-c1">0</span> <span class="pl-kos">}</span><span class="pl-kos">,</span> <span class="pl-s1">y</span><span class="pl-kos">,</span> <span class="pl-s1">b</span><span class="pl-kos">;</span>  
    <span class="pl-k">function</span> <span class="pl-en">n</span><span class="pl-kos">(</span><span class="pl-s1">c</span><span class="pl-kos">)</span> <span class="pl-kos">{</span>  
        <span class="pl-k">if</span> <span class="pl-kos">(</span><span class="pl-s1">f</span><span class="pl-kos">)</span> <span class="pl-k">throw</span> <span class="pl-k">new</span> <span class="pl-smi">TypeError</span><span class="pl-kos">(</span><span class="pl-s">"Generator is already executing."</span><span class="pl-kos">)</span><span class="pl-kos">;</span>  
        <span class="pl-k">switch</span> <span class="pl-kos">(</span><span class="pl-s1">d</span> <span class="pl-c1">&amp;&amp;</span> <span class="pl-s1">c</span><span class="pl-kos">[</span><span class="pl-c1">0</span><span class="pl-kos">]</span><span class="pl-kos">)</span> <span class="pl-kos">{</span>  
            <span class="pl-k">case</span> <span class="pl-c1">0</span> <span class="pl-c">/*next*/</span>: <span class="pl-k">return</span> <span class="pl-kos">{</span> <span class="pl-c1">value</span>: <span class="pl-k">void</span> <span class="pl-c1">0</span><span class="pl-kos">,</span> <span class="pl-c1">done</span>: <span class="pl-c1">true</span> <span class="pl-kos">}</span><span class="pl-kos">;</span>  
            <span class="pl-k">case</span> <span class="pl-c1">1</span> <span class="pl-c">/*throw*/</span>: <span class="pl-k">throw</span> <span class="pl-s1">c</span><span class="pl-kos">[</span><span class="pl-c1">1</span><span class="pl-kos">]</span><span class="pl-kos">;</span>  
            <span class="pl-k">case</span> <span class="pl-c1">2</span> <span class="pl-c">/*return*/</span>: <span class="pl-k">return</span> <span class="pl-kos">{</span> <span class="pl-c1">value</span>: <span class="pl-s1">c</span><span class="pl-kos">[</span><span class="pl-c1">1</span><span class="pl-kos">]</span><span class="pl-kos">,</span> <span class="pl-c1">done</span>: <span class="pl-c1">true</span> <span class="pl-kos">}</span><span class="pl-kos">;</span>  
        <span class="pl-kos">}</span>  
        <span class="pl-k">while</span> <span class="pl-kos">(</span><span class="pl-c1">true</span><span class="pl-kos">)</span> <span class="pl-kos">{</span>  
            <span class="pl-s1">f</span> <span class="pl-c1">=</span> <span class="pl-c1">false</span><span class="pl-kos">;</span>  
            <span class="pl-k">switch</span> <span class="pl-kos">(</span><span class="pl-c1">!</span><span class="pl-kos">(</span><span class="pl-s1">g</span> <span class="pl-c1">=</span> <span class="pl-s1">s</span><span class="pl-kos">.</span><span class="pl-c1">trys</span> <span class="pl-c1">&amp;&amp;</span> <span class="pl-s1">s</span><span class="pl-kos">.</span><span class="pl-c1">trys</span><span class="pl-kos">.</span><span class="pl-c1">length</span> <span class="pl-c1">&amp;&amp;</span> <span class="pl-s1">s</span><span class="pl-kos">.</span><span class="pl-c1">trys</span><span class="pl-kos">[</span><span class="pl-s1">s</span><span class="pl-kos">.</span><span class="pl-c1">trys</span><span class="pl-kos">.</span><span class="pl-c1">length</span> <span class="pl-c1">-</span> <span class="pl-c1">1</span><span class="pl-kos">]</span><span class="pl-kos">)</span> <span class="pl-c1">&amp;&amp;</span> <span class="pl-s1">c</span><span class="pl-kos">[</span><span class="pl-c1">0</span><span class="pl-kos">]</span><span class="pl-kos">)</span> <span class="pl-kos">{</span>  
                <span class="pl-k">case</span> <span class="pl-c1">1</span> <span class="pl-c">/*throw*/</span>: <span class="pl-s1">i</span><span class="pl-kos">.</span><span class="pl-c1">length</span> <span class="pl-c1">=</span> <span class="pl-c1">0</span><span class="pl-kos">;</span> <span class="pl-s1">d</span> <span class="pl-c1">=</span> <span class="pl-c1">true</span><span class="pl-kos">;</span> <span class="pl-k">throw</span> <span class="pl-s1">c</span><span class="pl-kos">[</span><span class="pl-c1">1</span><span class="pl-kos">]</span><span class="pl-kos">;</span>  
                <span class="pl-k">case</span> <span class="pl-c1">2</span> <span class="pl-c">/*return*/</span>: <span class="pl-s1">i</span><span class="pl-kos">.</span><span class="pl-c1">length</span> <span class="pl-c1">=</span> <span class="pl-c1">0</span><span class="pl-kos">;</span> <span class="pl-s1">d</span> <span class="pl-c1">=</span> <span class="pl-c1">true</span><span class="pl-kos">;</span> <span class="pl-k">return</span> <span class="pl-kos">{</span> <span class="pl-c1">value</span>: <span class="pl-s1">c</span><span class="pl-kos">[</span><span class="pl-c1">1</span><span class="pl-kos">]</span><span class="pl-kos">,</span> <span class="pl-c1">done</span>: <span class="pl-c1">true</span> <span class="pl-kos">}</span><span class="pl-kos">;</span>  
            <span class="pl-kos">}</span>  
            <span class="pl-k">try</span> <span class="pl-kos">{</span>  
                <span class="pl-k">if</span> <span class="pl-kos">(</span><span class="pl-s1">y</span><span class="pl-kos">)</span> <span class="pl-kos">{</span>  
                    <span class="pl-s1">f</span> <span class="pl-c1">=</span> <span class="pl-c1">true</span><span class="pl-kos">;</span>  
                    <span class="pl-k">if</span> <span class="pl-kos">(</span><span class="pl-k">typeof</span> <span class="pl-kos">(</span><span class="pl-s1">b</span> <span class="pl-c1">=</span> <span class="pl-s1">y</span><span class="pl-kos">[</span><span class="pl-s1">c</span><span class="pl-kos">[</span><span class="pl-c1">0</span><span class="pl-kos">]</span><span class="pl-kos">]</span><span class="pl-kos">)</span> <span class="pl-c1">===</span> <span class="pl-s">"function"</span><span class="pl-kos">)</span> <span class="pl-kos">{</span>  
                        <span class="pl-s1">b</span> <span class="pl-c1">=</span> <span class="pl-s1">b</span><span class="pl-kos">.</span><span class="pl-en">call</span><span class="pl-kos">(</span><span class="pl-s1">y</span><span class="pl-kos">,</span> <span class="pl-s1">c</span><span class="pl-kos">[</span><span class="pl-c1">1</span><span class="pl-kos">]</span><span class="pl-kos">)</span><span class="pl-kos">;</span>  
                        <span class="pl-k">if</span> <span class="pl-kos">(</span><span class="pl-c1">!</span><span class="pl-s1">b</span><span class="pl-kos">.</span><span class="pl-c1">done</span><span class="pl-kos">)</span> <span class="pl-k">return</span> <span class="pl-s1">b</span><span class="pl-kos">;</span>  
                        <span class="pl-s1">c</span><span class="pl-kos">[</span><span class="pl-c1">0</span><span class="pl-kos">]</span> <span class="pl-c1">=</span> <span class="pl-c1">0</span> <span class="pl-c">/*next*/</span><span class="pl-kos">,</span> <span class="pl-s1">c</span><span class="pl-kos">[</span><span class="pl-c1">1</span><span class="pl-kos">]</span> <span class="pl-c1">=</span> <span class="pl-s1">b</span><span class="pl-kos">.</span><span class="pl-c1">value</span><span class="pl-kos">;</span>  
                    <span class="pl-kos">}</span>  
                    <span class="pl-s1">y</span> <span class="pl-c1">=</span> <span class="pl-c1">undefined</span><span class="pl-kos">;</span>  
                    <span class="pl-s1">f</span> <span class="pl-c1">=</span> <span class="pl-c1">false</span><span class="pl-kos">;</span>  
                <span class="pl-kos">}</span>  
                <span class="pl-k">switch</span> <span class="pl-kos">(</span><span class="pl-s1">c</span><span class="pl-kos">[</span><span class="pl-c1">0</span><span class="pl-kos">]</span><span class="pl-kos">)</span> <span class="pl-kos">{</span>  
                    <span class="pl-k">case</span> <span class="pl-c1">0</span> <span class="pl-c">/*next*/</span>: <span class="pl-s1">s</span><span class="pl-kos">.</span><span class="pl-c1">sent</span> <span class="pl-c1">=</span> <span class="pl-s1">c</span><span class="pl-kos">[</span><span class="pl-c1">1</span><span class="pl-kos">]</span><span class="pl-kos">;</span> <span class="pl-k">break</span><span class="pl-kos">;</span>  
                    <span class="pl-k">case</span> <span class="pl-c1">3</span> <span class="pl-c">/*yield*/</span>: <span class="pl-s1">s</span><span class="pl-kos">.</span><span class="pl-c1">label</span><span class="pl-c1">++</span><span class="pl-kos">;</span> <span class="pl-k">return</span> <span class="pl-kos">{</span> <span class="pl-c1">value</span>: <span class="pl-s1">c</span><span class="pl-kos">[</span><span class="pl-c1">1</span><span class="pl-kos">]</span><span class="pl-kos">,</span> <span class="pl-c1">done</span>: <span class="pl-c1">false</span> <span class="pl-kos">}</span><span class="pl-kos">;</span>  
                    <span class="pl-k">case</span> <span class="pl-c1">4</span> <span class="pl-c">/*yield**/</span>: <span class="pl-s1">s</span><span class="pl-kos">.</span><span class="pl-c1">label</span><span class="pl-c1">++</span><span class="pl-kos">;</span> <span class="pl-s1">y</span> <span class="pl-c1">=</span> <span class="pl-s1">c</span><span class="pl-kos">[</span><span class="pl-c1">1</span><span class="pl-kos">]</span><span class="pl-kos">;</span> <span class="pl-s1">c</span><span class="pl-kos">[</span><span class="pl-c1">0</span><span class="pl-kos">]</span> <span class="pl-c1">=</span> <span class="pl-c1">0</span> <span class="pl-c">/*next*/</span><span class="pl-kos">;</span> <span class="pl-s1">c</span><span class="pl-kos">[</span><span class="pl-c1">1</span><span class="pl-kos">]</span> <span class="pl-c1">=</span> <span class="pl-k">void</span> <span class="pl-c1">0</span><span class="pl-kos">;</span> <span class="pl-k">continue</span><span class="pl-kos">;</span>  
                    <span class="pl-k">case</span> <span class="pl-c1">6</span> <span class="pl-c">/*endfinally*/</span>: <span class="pl-s1">c</span> <span class="pl-c1">=</span> <span class="pl-s1">i</span><span class="pl-kos">.</span><span class="pl-en">pop</span><span class="pl-kos">(</span><span class="pl-kos">)</span><span class="pl-kos">;</span> <span class="pl-k">continue</span><span class="pl-kos">;</span>  
                    <span class="pl-k">default</span>:  
                        <span class="pl-k">if</span> <span class="pl-kos">(</span><span class="pl-s1">c</span><span class="pl-kos">[</span><span class="pl-c1">0</span><span class="pl-kos">]</span> <span class="pl-c1">===</span> <span class="pl-c1">1</span> <span class="pl-c">/*throw*/</span> <span class="pl-c1">&amp;&amp;</span> <span class="pl-s1">s</span><span class="pl-kos">.</span><span class="pl-c1">label</span> <span class="pl-c1">&lt;</span> <span class="pl-s1">g</span><span class="pl-kos">[</span><span class="pl-c1">1</span><span class="pl-kos">]</span><span class="pl-kos">)</span> <span class="pl-kos">{</span> <span class="pl-s1">s</span><span class="pl-kos">.</span><span class="pl-c1">error</span> <span class="pl-c1">=</span> <span class="pl-s1">c</span><span class="pl-kos">[</span><span class="pl-c1">1</span><span class="pl-kos">]</span><span class="pl-kos">;</span> <span class="pl-s1">s</span><span class="pl-kos">.</span><span class="pl-c1">label</span> <span class="pl-c1">=</span> <span class="pl-s1">g</span><span class="pl-kos">[</span><span class="pl-c1">1</span><span class="pl-kos">]</span><span class="pl-kos">;</span> <span class="pl-k">break</span><span class="pl-kos">;</span> <span class="pl-kos">}</span>  
                        <span class="pl-k">if</span> <span class="pl-kos">(</span><span class="pl-s1">c</span><span class="pl-kos">[</span><span class="pl-c1">0</span><span class="pl-kos">]</span> <span class="pl-c1">===</span> <span class="pl-c1">5</span> <span class="pl-c">/*break*/</span> <span class="pl-c1">&amp;&amp;</span> <span class="pl-kos">(</span><span class="pl-c1">!</span><span class="pl-s1">g</span> <span class="pl-c1">||</span> <span class="pl-kos">(</span><span class="pl-s1">c</span><span class="pl-kos">[</span><span class="pl-c1">1</span><span class="pl-kos">]</span> <span class="pl-c1">&gt;=</span> <span class="pl-s1">g</span><span class="pl-kos">[</span><span class="pl-c1">0</span><span class="pl-kos">]</span> <span class="pl-c1">&amp;&amp;</span> <span class="pl-s1">c</span><span class="pl-kos">[</span><span class="pl-c1">1</span><span class="pl-kos">]</span> <span class="pl-c1">&lt;</span> <span class="pl-s1">g</span><span class="pl-kos">[</span><span class="pl-c1">3</span><span class="pl-kos">]</span><span class="pl-kos">)</span><span class="pl-kos">)</span><span class="pl-kos">)</span> <span class="pl-kos">{</span> <span class="pl-s1">s</span><span class="pl-kos">.</span><span class="pl-c1">label</span> <span class="pl-c1">=</span> <span class="pl-s1">c</span><span class="pl-kos">[</span><span class="pl-c1">1</span><span class="pl-kos">]</span><span class="pl-kos">;</span> <span class="pl-k">break</span><span class="pl-kos">;</span> <span class="pl-kos">}</span>  
                        <span class="pl-s1">s</span><span class="pl-kos">.</span><span class="pl-c1">trys</span><span class="pl-kos">.</span><span class="pl-en">pop</span><span class="pl-kos">(</span><span class="pl-kos">)</span><span class="pl-kos">;</span>  
                        <span class="pl-k">if</span> <span class="pl-kos">(</span><span class="pl-s1">g</span><span class="pl-kos">[</span><span class="pl-c1">2</span><span class="pl-kos">]</span><span class="pl-kos">)</span> <span class="pl-kos">{</span> <span class="pl-s1">i</span><span class="pl-kos">.</span><span class="pl-en">push</span><span class="pl-kos">(</span><span class="pl-s1">c</span><span class="pl-kos">)</span><span class="pl-kos">;</span> <span class="pl-s1">s</span><span class="pl-kos">.</span><span class="pl-c1">label</span> <span class="pl-c1">=</span> <span class="pl-s1">g</span><span class="pl-kos">[</span><span class="pl-c1">2</span><span class="pl-kos">]</span><span class="pl-kos">;</span> <span class="pl-k">break</span><span class="pl-kos">;</span> <span class="pl-kos">}</span>  
                        <span class="pl-k">continue</span><span class="pl-kos">;</span>  
                <span class="pl-kos">}</span>  
                <span class="pl-s1">f</span> <span class="pl-c1">=</span> <span class="pl-c1">true</span><span class="pl-kos">;</span>  
                <span class="pl-s1">c</span> <span class="pl-c1">=</span> <span class="pl-en">m</span><span class="pl-kos">(</span><span class="pl-s1">s</span><span class="pl-kos">)</span><span class="pl-kos">;</span>  
            <span class="pl-kos">}</span> <span class="pl-k">catch</span> <span class="pl-kos">(</span><span class="pl-s1">e</span><span class="pl-kos">)</span> <span class="pl-kos">{</span>  
                <span class="pl-s1">y</span> <span class="pl-c1">=</span> <span class="pl-k">void</span> <span class="pl-c1">0</span><span class="pl-kos">;</span>  
                <span class="pl-s1">c</span><span class="pl-kos">[</span><span class="pl-c1">0</span><span class="pl-kos">]</span> <span class="pl-c1">=</span> <span class="pl-c1">1</span> <span class="pl-c">/*throw*/</span><span class="pl-kos">,</span> <span class="pl-s1">c</span><span class="pl-kos">[</span><span class="pl-c1">1</span><span class="pl-kos">]</span> <span class="pl-c1">=</span> <span class="pl-s1">e</span><span class="pl-kos">;</span>  
            <span class="pl-kos">}</span>  
        <span class="pl-kos">}</span>  
    <span class="pl-kos">}</span>  
    <span class="pl-k">return</span> <span class="pl-kos">{</span>  
        <span class="pl-en">next</span>: <span class="pl-k">function</span> <span class="pl-kos">(</span><span class="pl-s1">v</span><span class="pl-kos">)</span> <span class="pl-kos">{</span> <span class="pl-k">return</span> <span class="pl-en">n</span><span class="pl-kos">(</span><span class="pl-kos">[</span><span class="pl-c1">0</span> <span class="pl-c">/*next*/</span><span class="pl-kos">,</span> <span class="pl-s1">v</span><span class="pl-kos">]</span><span class="pl-kos">)</span><span class="pl-kos">;</span> <span class="pl-kos">}</span><span class="pl-kos">,</span>  
        <span class="pl-c1">1</span> <span class="pl-c">/*throw*/</span>: <span class="pl-k">function</span> <span class="pl-kos">(</span><span class="pl-s1">v</span><span class="pl-kos">)</span> <span class="pl-kos">{</span> <span class="pl-k">return</span> <span class="pl-en">n</span><span class="pl-kos">(</span><span class="pl-kos">[</span><span class="pl-c1">1</span> <span class="pl-c">/*throw*/</span><span class="pl-kos">,</span> <span class="pl-s1">v</span><span class="pl-kos">]</span><span class="pl-kos">)</span><span class="pl-kos">;</span> <span class="pl-kos">}</span><span class="pl-kos">,</span>  
        <span class="pl-c1">2</span> <span class="pl-c">/*return*/</span>: <span class="pl-k">function</span> <span class="pl-kos">(</span><span class="pl-s1">v</span><span class="pl-kos">)</span> <span class="pl-kos">{</span> <span class="pl-k">return</span> <span class="pl-en">n</span><span class="pl-kos">(</span><span class="pl-kos">[</span><span class="pl-c1">2</span> <span class="pl-c">/*return*/</span><span class="pl-kos">,</span> <span class="pl-s1">v</span><span class="pl-kos">]</span><span class="pl-kos">)</span><span class="pl-kos">;</span> <span class="pl-kos">}</span><span class="pl-kos">,</span>  
    <span class="pl-kos">}</span><span class="pl-kos">;</span>  
<span class="pl-kos">}</span><span class="pl-kos">;</span></pre></div>
<h3 dir="auto"><a name="user-content-b.2.1">B.2.1 <code class="notranslate">_generator</code>  Arguments</a></h3>
<table role="table">
<thead>
<tr>
<th>argument</th>
<th>description</th>
</tr>
</thead>
<tbody>
<tr>
<td><code class="notranslate">m</code></td>
<td>State machine function.</td>
</tr>
</tbody>
</table>
<h3 dir="auto"><a name="user-content-b.2.2">B.2.2  <code class="notranslate">n</code>  Arguments</a></h3>
<table role="table">
<thead>
<tr>
<th>argument</th>
<th>description</th>
</tr>
</thead>
<tbody>
<tr>
<td><code class="notranslate">c</code></td>
<td>The current instruction.</td>
</tr>
</tbody>
</table>
<h3 dir="auto"><a name="user-content-b.2.3">B.2.3 Variables</a></h3>
<table role="table">
<thead>
<tr>
<th>variable</th>
<th>description</th>
</tr>
</thead>
<tbody>
<tr>
<td><code class="notranslate">d</code></td>
<td>A value indicating whether the generator is done executing.</td>
</tr>
<tr>
<td><code class="notranslate">i</code></td>
<td>A stack of instructions (see below) pending execution.</td>
</tr>
<tr>
<td><code class="notranslate">f</code></td>
<td>A value indicating whether the generator is executing, to prevent reentry (per ES6 spec).</td>
</tr>
<tr>
<td><code class="notranslate">g</code></td>
<td>The region at the top of the <code class="notranslate">s.trys</code> stack.</td>
</tr>
<tr>
<td><code class="notranslate">s</code></td>
<td>State information for the state machine function.</td>
</tr>
<tr>
<td><code class="notranslate">s.label</code></td>
<td>The label for the next instruction.</td>
</tr>
<tr>
<td><code class="notranslate">s.trys</code></td>
<td>An optional stack of protected regions for <code class="notranslate">try..catch..finally</code> blocks.</td>
</tr>
<tr>
<td><code class="notranslate">s.sent</code></td>
<td>A value sent to the generator when calling <code class="notranslate">next</code>.</td>
</tr>
<tr>
<td><code class="notranslate">s.error</code></td>
<td>An error caught by a <code class="notranslate">catch</code> clause.</td>
</tr>
<tr>
<td><code class="notranslate">y</code></td>
<td>The current inner generator to which to delegate generator instructions.</td>
</tr>
<tr>
<td><code class="notranslate">b</code></td>
<td>The method on <code class="notranslate">n</code> to execute for the current instruction. One of <code class="notranslate">"next"</code>, <code class="notranslate">"throw"</code>, or <code class="notranslate">"return"</code>.</td>
</tr>
</tbody>
</table>
<h3 dir="auto"><a name="user-content-b.2.4">B.2.4 Instructions</a></h3>
<table role="table">
<thead>
<tr>
<th>instruction</th>
<th align="center">args</th>
<th>description</th>
</tr>
</thead>
<tbody>
<tr>
<td><code class="notranslate">0 /*next*/</code></td>
<td align="center">0-1</td>
<td>Begin or resume processing with an optional sent value.</td>
</tr>
<tr>
<td><code class="notranslate">1 /*throw*/</code></td>
<td align="center">1</td>
<td>Throw an exception at the current instruction. Executes any enclosing <code class="notranslate">catch</code> or <code class="notranslate">finally</code> blocks.</td>
</tr>
<tr>
<td><code class="notranslate">2 /*return*/</code></td>
<td align="center">0-1</td>
<td>Returns a value from the current instruction. Executes any enclosing <code class="notranslate">finally</code> blocks.</td>
</tr>
<tr>
<td><code class="notranslate">3 /*yield*/</code></td>
<td align="center">0-1</td>
<td>Suspends execution and yields an optional value to the caller of the generator.</td>
</tr>
<tr>
<td><code class="notranslate">4 /*yield**/</code></td>
<td align="center">1</td>
<td>Delegates generator operations to the provided iterable (not needed for <em>Async Functions</em>, but provided for future support for down-level <em>Generator Functions</em>).</td>
</tr>
<tr>
<td><code class="notranslate">5 /*break*/</code></td>
<td align="center">1</td>
<td>Jumps to a labeled instruction. Executes any enclosing <code class="notranslate">finally</code> blocks if the target is outside of the current protected region.</td>
</tr>
<tr>
<td><code class="notranslate">6 /*endfinally*/</code></td>
<td align="center">0</td>
<td>Marks the end of a <code class="notranslate">finally</code> block so that the previous <code class="notranslate">break</code>, <code class="notranslate">throw</code>, or <code class="notranslate">return</code> instruction can be processed.</td>
</tr>
</tbody>
</table>
<h3 dir="auto"><a name="user-content-b.2.5">B.2.5 Protected Regions</a></h3>
<p dir="auto">A protected region marks the beginning and end of a <code class="notranslate">try..catch</code> or <code class="notranslate">try..finally</code> block. Protected regions are pushed onto the <code class="notranslate">s.trys</code> stack whenever a protected region is entered when executing the state machine. A protected region is defined using a quadruple in the following format:</p>
<table role="table">
<thead>
<tr>
<th>field</th>
<th>required</th>
<th>description</th>
</tr>
</thead>
<tbody>
<tr>
<td>0</td>
<td>yes</td>
<td>The start of a <code class="notranslate">try</code> block.</td>
</tr>
<tr>
<td>1</td>
<td>no</td>
<td>The start of a <code class="notranslate">catch</code> block.</td>
</tr>
<tr>
<td>2</td>
<td>no</td>
<td>The start of a <code class="notranslate">finally</code> block.</td>
</tr>
<tr>
<td>3</td>
<td>yes</td>
<td>The end of a <code class="notranslate">try..catch..finally</code> block.</td>
</tr>
</tbody>
</table>
<h2 dir="auto"><a name="user-content-b.3">B.3 <code class="notranslate">__generator</code> helper function (alternate)</a></h2>
<div class="highlight highlight-source-ts notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="var __generator = __generator || function (body) {  
    var done, instructions = [], stepping, region, state = { label: 0 }, delegated;  
    function step(instruction) {  
        if (stepping) throw new TypeError(&quot;Generator is already executing.&quot;);  
        if (done) {  
            switch (instruction[0]) {  
                case 0 /*next*/: return { value: void 0, done: true };  
                case 1 /*throw*/: throw instruction[1];  
                case 2 /*return*/: return { value: instruction[1], done: true };  
            }  
        }  
        while (true) {  
            stepping = false;  
            var region = state.trys &amp;&amp; state.trys.length &amp;&amp; state.trys[state.trys.length - 1];  
            if (region) {  
                switch (instruction[0]) {  
                    case 1 /*throw*/:  
                        instructions.length = 0;  
                        done = true;  
                        throw instruction[1];  
                    case 2 /*return*/:  
                        instructions.length = 0;  
                        done = true;  
                        return { value: instruction[1], done: true };  
                }  
            }  
            try {  
                if (delegated) {  
                    stepping = true;  
                    var callback = delegated[instruction[0]];  
                    if (typeof callback === &quot;function&quot;) {  
                        var result = callback.call(delegated, instruction[1]);  
                        if (!result.done) return result;  
                        instruction[0] = 0 /*next*/;  
                        instruction[1] = result.value;  
                    }  
                    delegated = undefined;  
                    stepping = false;  
                }  
                switch (instruction[0]) {  
                    case 3 /*yield*/:  
                        state.label++;  
                        return { value: instruction[1], done: false };  
                    case 4 /*yield**/:  
                        state.label++;  
                        delegated = instruction[1];  
                        instruction[0] = 0 /*next*/;  
                        instruction[1] = void 0;  
                        continue;  
                    case 0 /*next*/:  
                        state.sent = instruction[1];  
                        break;  
                    case 6 /*endfinally*/:  
                        instruction = instructions.pop();  
                        continue;  
                    default:  
                        if (instruction[0] === 5 /*break*/ &amp;&amp; (!region || (instruction[1] &gt;= region[0] &amp;&amp; instruction[1] &lt; region[3]))) {  
                            state.label = instruction[1];  
                            break;  
                        }  
                        if (instruction[0] === 1 /*throw*/ &amp;&amp; state.label &lt; region[1]) {  
                            state.error = instruction[1];  
                            state.label = region[1];  
                            break;  
                        }  
                        state.trys.pop();  
                        if (region[2]) {  
                            instructions.push(instruction);  
                            state.label = region[2];  
                            break;  
                        }  
                        continue;  
                }  
                stepping = true;  
                instruction = body(state);  
            } catch (e) {  
                delegated = void 0;  
                instruction[0] = 1 /*throw*/, instruction[1] = e;  
            }  
        }  
    }  
    return {  
        next: function (v) { return step([0 /*next*/, v]); },  
        &quot;throw&quot;: function (v) { return step([1 /*throw*/, v]); },  
        &quot;return&quot;: function (v) { return step([2 /*return*/, v]); },  
    };  
};"><pre class="notranslate"><span class="pl-k">var</span> <span class="pl-s1">__generator</span> <span class="pl-c1">=</span> <span class="pl-s1">__generator</span> <span class="pl-c1">||</span> <span class="pl-k">function</span> <span class="pl-kos">(</span><span class="pl-s1">body</span><span class="pl-kos">)</span> <span class="pl-kos">{</span>  
    <span class="pl-k">var</span> <span class="pl-s1">done</span><span class="pl-kos">,</span> <span class="pl-s1">instructions</span> <span class="pl-c1">=</span> <span class="pl-kos">[</span><span class="pl-kos">]</span><span class="pl-kos">,</span> <span class="pl-s1">stepping</span><span class="pl-kos">,</span> <span class="pl-s1">region</span><span class="pl-kos">,</span> <span class="pl-s1">state</span> <span class="pl-c1">=</span> <span class="pl-kos">{</span> <span class="pl-c1">label</span>: <span class="pl-c1">0</span> <span class="pl-kos">}</span><span class="pl-kos">,</span> <span class="pl-s1">delegated</span><span class="pl-kos">;</span>  
    <span class="pl-k">function</span> <span class="pl-en">step</span><span class="pl-kos">(</span><span class="pl-s1">instruction</span><span class="pl-kos">)</span> <span class="pl-kos">{</span>  
        <span class="pl-k">if</span> <span class="pl-kos">(</span><span class="pl-s1">stepping</span><span class="pl-kos">)</span> <span class="pl-k">throw</span> <span class="pl-k">new</span> <span class="pl-smi">TypeError</span><span class="pl-kos">(</span><span class="pl-s">"Generator is already executing."</span><span class="pl-kos">)</span><span class="pl-kos">;</span>  
        <span class="pl-k">if</span> <span class="pl-kos">(</span><span class="pl-s1">done</span><span class="pl-kos">)</span> <span class="pl-kos">{</span>  
            <span class="pl-k">switch</span> <span class="pl-kos">(</span><span class="pl-s1">instruction</span><span class="pl-kos">[</span><span class="pl-c1">0</span><span class="pl-kos">]</span><span class="pl-kos">)</span> <span class="pl-kos">{</span>  
                <span class="pl-k">case</span> <span class="pl-c1">0</span> <span class="pl-c">/*next*/</span>: <span class="pl-k">return</span> <span class="pl-kos">{</span> <span class="pl-c1">value</span>: <span class="pl-k">void</span> <span class="pl-c1">0</span><span class="pl-kos">,</span> <span class="pl-c1">done</span>: <span class="pl-c1">true</span> <span class="pl-kos">}</span><span class="pl-kos">;</span>  
                <span class="pl-k">case</span> <span class="pl-c1">1</span> <span class="pl-c">/*throw*/</span>: <span class="pl-k">throw</span> <span class="pl-s1">instruction</span><span class="pl-kos">[</span><span class="pl-c1">1</span><span class="pl-kos">]</span><span class="pl-kos">;</span>  
                <span class="pl-k">case</span> <span class="pl-c1">2</span> <span class="pl-c">/*return*/</span>: <span class="pl-k">return</span> <span class="pl-kos">{</span> <span class="pl-c1">value</span>: <span class="pl-s1">instruction</span><span class="pl-kos">[</span><span class="pl-c1">1</span><span class="pl-kos">]</span><span class="pl-kos">,</span> <span class="pl-c1">done</span>: <span class="pl-c1">true</span> <span class="pl-kos">}</span><span class="pl-kos">;</span>  
            <span class="pl-kos">}</span>  
        <span class="pl-kos">}</span>  
        <span class="pl-k">while</span> <span class="pl-kos">(</span><span class="pl-c1">true</span><span class="pl-kos">)</span> <span class="pl-kos">{</span>  
            <span class="pl-s1">stepping</span> <span class="pl-c1">=</span> <span class="pl-c1">false</span><span class="pl-kos">;</span>  
            <span class="pl-k">var</span> <span class="pl-s1">region</span> <span class="pl-c1">=</span> <span class="pl-s1">state</span><span class="pl-kos">.</span><span class="pl-c1">trys</span> <span class="pl-c1">&amp;&amp;</span> <span class="pl-s1">state</span><span class="pl-kos">.</span><span class="pl-c1">trys</span><span class="pl-kos">.</span><span class="pl-c1">length</span> <span class="pl-c1">&amp;&amp;</span> <span class="pl-s1">state</span><span class="pl-kos">.</span><span class="pl-c1">trys</span><span class="pl-kos">[</span><span class="pl-s1">state</span><span class="pl-kos">.</span><span class="pl-c1">trys</span><span class="pl-kos">.</span><span class="pl-c1">length</span> <span class="pl-c1">-</span> <span class="pl-c1">1</span><span class="pl-kos">]</span><span class="pl-kos">;</span>  
            <span class="pl-k">if</span> <span class="pl-kos">(</span><span class="pl-s1">region</span><span class="pl-kos">)</span> <span class="pl-kos">{</span>  
                <span class="pl-k">switch</span> <span class="pl-kos">(</span><span class="pl-s1">instruction</span><span class="pl-kos">[</span><span class="pl-c1">0</span><span class="pl-kos">]</span><span class="pl-kos">)</span> <span class="pl-kos">{</span>  
                    <span class="pl-k">case</span> <span class="pl-c1">1</span> <span class="pl-c">/*throw*/</span>:  
                        <span class="pl-s1">instructions</span><span class="pl-kos">.</span><span class="pl-c1">length</span> <span class="pl-c1">=</span> <span class="pl-c1">0</span><span class="pl-kos">;</span>  
                        <span class="pl-s1">done</span> <span class="pl-c1">=</span> <span class="pl-c1">true</span><span class="pl-kos">;</span>  
                        <span class="pl-k">throw</span> <span class="pl-s1">instruction</span><span class="pl-kos">[</span><span class="pl-c1">1</span><span class="pl-kos">]</span><span class="pl-kos">;</span>  
                    <span class="pl-k">case</span> <span class="pl-c1">2</span> <span class="pl-c">/*return*/</span>:  
                        <span class="pl-s1">instructions</span><span class="pl-kos">.</span><span class="pl-c1">length</span> <span class="pl-c1">=</span> <span class="pl-c1">0</span><span class="pl-kos">;</span>  
                        <span class="pl-s1">done</span> <span class="pl-c1">=</span> <span class="pl-c1">true</span><span class="pl-kos">;</span>  
                        <span class="pl-k">return</span> <span class="pl-kos">{</span> <span class="pl-c1">value</span>: <span class="pl-s1">instruction</span><span class="pl-kos">[</span><span class="pl-c1">1</span><span class="pl-kos">]</span><span class="pl-kos">,</span> <span class="pl-c1">done</span>: <span class="pl-c1">true</span> <span class="pl-kos">}</span><span class="pl-kos">;</span>  
                <span class="pl-kos">}</span>  
            <span class="pl-kos">}</span>  
            <span class="pl-k">try</span> <span class="pl-kos">{</span>  
                <span class="pl-k">if</span> <span class="pl-kos">(</span><span class="pl-s1">delegated</span><span class="pl-kos">)</span> <span class="pl-kos">{</span>  
                    <span class="pl-s1">stepping</span> <span class="pl-c1">=</span> <span class="pl-c1">true</span><span class="pl-kos">;</span>  
                    <span class="pl-k">var</span> <span class="pl-s1">callback</span> <span class="pl-c1">=</span> <span class="pl-s1">delegated</span><span class="pl-kos">[</span><span class="pl-s1">instruction</span><span class="pl-kos">[</span><span class="pl-c1">0</span><span class="pl-kos">]</span><span class="pl-kos">]</span><span class="pl-kos">;</span>  
                    <span class="pl-k">if</span> <span class="pl-kos">(</span><span class="pl-k">typeof</span> <span class="pl-s1">callback</span> <span class="pl-c1">===</span> <span class="pl-s">"function"</span><span class="pl-kos">)</span> <span class="pl-kos">{</span>  
                        <span class="pl-k">var</span> <span class="pl-s1">result</span> <span class="pl-c1">=</span> <span class="pl-s1">callback</span><span class="pl-kos">.</span><span class="pl-en">call</span><span class="pl-kos">(</span><span class="pl-s1">delegated</span><span class="pl-kos">,</span> <span class="pl-s1">instruction</span><span class="pl-kos">[</span><span class="pl-c1">1</span><span class="pl-kos">]</span><span class="pl-kos">)</span><span class="pl-kos">;</span>  
                        <span class="pl-k">if</span> <span class="pl-kos">(</span><span class="pl-c1">!</span><span class="pl-s1">result</span><span class="pl-kos">.</span><span class="pl-c1">done</span><span class="pl-kos">)</span> <span class="pl-k">return</span> <span class="pl-s1">result</span><span class="pl-kos">;</span>  
                        <span class="pl-s1">instruction</span><span class="pl-kos">[</span><span class="pl-c1">0</span><span class="pl-kos">]</span> <span class="pl-c1">=</span> <span class="pl-c1">0</span> <span class="pl-c">/*next*/</span><span class="pl-kos">;</span>  
                        <span class="pl-s1">instruction</span><span class="pl-kos">[</span><span class="pl-c1">1</span><span class="pl-kos">]</span> <span class="pl-c1">=</span> <span class="pl-s1">result</span><span class="pl-kos">.</span><span class="pl-c1">value</span><span class="pl-kos">;</span>  
                    <span class="pl-kos">}</span>  
                    <span class="pl-s1">delegated</span> <span class="pl-c1">=</span> <span class="pl-c1">undefined</span><span class="pl-kos">;</span>  
                    <span class="pl-s1">stepping</span> <span class="pl-c1">=</span> <span class="pl-c1">false</span><span class="pl-kos">;</span>  
                <span class="pl-kos">}</span>  
                <span class="pl-k">switch</span> <span class="pl-kos">(</span><span class="pl-s1">instruction</span><span class="pl-kos">[</span><span class="pl-c1">0</span><span class="pl-kos">]</span><span class="pl-kos">)</span> <span class="pl-kos">{</span>  
                    <span class="pl-k">case</span> <span class="pl-c1">3</span> <span class="pl-c">/*yield*/</span>:  
                        <span class="pl-s1">state</span><span class="pl-kos">.</span><span class="pl-c1">label</span><span class="pl-c1">++</span><span class="pl-kos">;</span>  
                        <span class="pl-k">return</span> <span class="pl-kos">{</span> <span class="pl-c1">value</span>: <span class="pl-s1">instruction</span><span class="pl-kos">[</span><span class="pl-c1">1</span><span class="pl-kos">]</span><span class="pl-kos">,</span> <span class="pl-c1">done</span>: <span class="pl-c1">false</span> <span class="pl-kos">}</span><span class="pl-kos">;</span>  
                    <span class="pl-k">case</span> <span class="pl-c1">4</span> <span class="pl-c">/*yield**/</span>:  
                        <span class="pl-s1">state</span><span class="pl-kos">.</span><span class="pl-c1">label</span><span class="pl-c1">++</span><span class="pl-kos">;</span>  
                        <span class="pl-s1">delegated</span> <span class="pl-c1">=</span> <span class="pl-s1">instruction</span><span class="pl-kos">[</span><span class="pl-c1">1</span><span class="pl-kos">]</span><span class="pl-kos">;</span>  
                        <span class="pl-s1">instruction</span><span class="pl-kos">[</span><span class="pl-c1">0</span><span class="pl-kos">]</span> <span class="pl-c1">=</span> <span class="pl-c1">0</span> <span class="pl-c">/*next*/</span><span class="pl-kos">;</span>  
                        <span class="pl-s1">instruction</span><span class="pl-kos">[</span><span class="pl-c1">1</span><span class="pl-kos">]</span> <span class="pl-c1">=</span> <span class="pl-k">void</span> <span class="pl-c1">0</span><span class="pl-kos">;</span>  
                        <span class="pl-k">continue</span><span class="pl-kos">;</span>  
                    <span class="pl-k">case</span> <span class="pl-c1">0</span> <span class="pl-c">/*next*/</span>:  
                        <span class="pl-s1">state</span><span class="pl-kos">.</span><span class="pl-c1">sent</span> <span class="pl-c1">=</span> <span class="pl-s1">instruction</span><span class="pl-kos">[</span><span class="pl-c1">1</span><span class="pl-kos">]</span><span class="pl-kos">;</span>  
                        <span class="pl-k">break</span><span class="pl-kos">;</span>  
                    <span class="pl-k">case</span> <span class="pl-c1">6</span> <span class="pl-c">/*endfinally*/</span>:  
                        <span class="pl-s1">instruction</span> <span class="pl-c1">=</span> <span class="pl-s1">instructions</span><span class="pl-kos">.</span><span class="pl-en">pop</span><span class="pl-kos">(</span><span class="pl-kos">)</span><span class="pl-kos">;</span>  
                        <span class="pl-k">continue</span><span class="pl-kos">;</span>  
                    <span class="pl-k">default</span>:  
                        <span class="pl-k">if</span> <span class="pl-kos">(</span><span class="pl-s1">instruction</span><span class="pl-kos">[</span><span class="pl-c1">0</span><span class="pl-kos">]</span> <span class="pl-c1">===</span> <span class="pl-c1">5</span> <span class="pl-c">/*break*/</span> <span class="pl-c1">&amp;&amp;</span> <span class="pl-kos">(</span><span class="pl-c1">!</span><span class="pl-s1">region</span> <span class="pl-c1">||</span> <span class="pl-kos">(</span><span class="pl-s1">instruction</span><span class="pl-kos">[</span><span class="pl-c1">1</span><span class="pl-kos">]</span> <span class="pl-c1">&gt;=</span> <span class="pl-s1">region</span><span class="pl-kos">[</span><span class="pl-c1">0</span><span class="pl-kos">]</span> <span class="pl-c1">&amp;&amp;</span> <span class="pl-s1">instruction</span><span class="pl-kos">[</span><span class="pl-c1">1</span><span class="pl-kos">]</span> <span class="pl-c1">&lt;</span> <span class="pl-s1">region</span><span class="pl-kos">[</span><span class="pl-c1">3</span><span class="pl-kos">]</span><span class="pl-kos">)</span><span class="pl-kos">)</span><span class="pl-kos">)</span> <span class="pl-kos">{</span>  
                            <span class="pl-s1">state</span><span class="pl-kos">.</span><span class="pl-c1">label</span> <span class="pl-c1">=</span> <span class="pl-s1">instruction</span><span class="pl-kos">[</span><span class="pl-c1">1</span><span class="pl-kos">]</span><span class="pl-kos">;</span>  
                            <span class="pl-k">break</span><span class="pl-kos">;</span>  
                        <span class="pl-kos">}</span>  
                        <span class="pl-k">if</span> <span class="pl-kos">(</span><span class="pl-s1">instruction</span><span class="pl-kos">[</span><span class="pl-c1">0</span><span class="pl-kos">]</span> <span class="pl-c1">===</span> <span class="pl-c1">1</span> <span class="pl-c">/*throw*/</span> <span class="pl-c1">&amp;&amp;</span> <span class="pl-s1">state</span><span class="pl-kos">.</span><span class="pl-c1">label</span> <span class="pl-c1">&lt;</span> <span class="pl-s1">region</span><span class="pl-kos">[</span><span class="pl-c1">1</span><span class="pl-kos">]</span><span class="pl-kos">)</span> <span class="pl-kos">{</span>  
                            <span class="pl-s1">state</span><span class="pl-kos">.</span><span class="pl-c1">error</span> <span class="pl-c1">=</span> <span class="pl-s1">instruction</span><span class="pl-kos">[</span><span class="pl-c1">1</span><span class="pl-kos">]</span><span class="pl-kos">;</span>  
                            <span class="pl-s1">state</span><span class="pl-kos">.</span><span class="pl-c1">label</span> <span class="pl-c1">=</span> <span class="pl-s1">region</span><span class="pl-kos">[</span><span class="pl-c1">1</span><span class="pl-kos">]</span><span class="pl-kos">;</span>  
                            <span class="pl-k">break</span><span class="pl-kos">;</span>  
                        <span class="pl-kos">}</span>  
                        <span class="pl-s1">state</span><span class="pl-kos">.</span><span class="pl-c1">trys</span><span class="pl-kos">.</span><span class="pl-en">pop</span><span class="pl-kos">(</span><span class="pl-kos">)</span><span class="pl-kos">;</span>  
                        <span class="pl-k">if</span> <span class="pl-kos">(</span><span class="pl-s1">region</span><span class="pl-kos">[</span><span class="pl-c1">2</span><span class="pl-kos">]</span><span class="pl-kos">)</span> <span class="pl-kos">{</span>  
                            <span class="pl-s1">instructions</span><span class="pl-kos">.</span><span class="pl-en">push</span><span class="pl-kos">(</span><span class="pl-s1">instruction</span><span class="pl-kos">)</span><span class="pl-kos">;</span>  
                            <span class="pl-s1">state</span><span class="pl-kos">.</span><span class="pl-c1">label</span> <span class="pl-c1">=</span> <span class="pl-s1">region</span><span class="pl-kos">[</span><span class="pl-c1">2</span><span class="pl-kos">]</span><span class="pl-kos">;</span>  
                            <span class="pl-k">break</span><span class="pl-kos">;</span>  
                        <span class="pl-kos">}</span>  
                        <span class="pl-k">continue</span><span class="pl-kos">;</span>  
                <span class="pl-kos">}</span>  
                <span class="pl-s1">stepping</span> <span class="pl-c1">=</span> <span class="pl-c1">true</span><span class="pl-kos">;</span>  
                <span class="pl-s1">instruction</span> <span class="pl-c1">=</span> <span class="pl-en">body</span><span class="pl-kos">(</span><span class="pl-s1">state</span><span class="pl-kos">)</span><span class="pl-kos">;</span>  
            <span class="pl-kos">}</span> <span class="pl-k">catch</span> <span class="pl-kos">(</span><span class="pl-s1">e</span><span class="pl-kos">)</span> <span class="pl-kos">{</span>  
                <span class="pl-s1">delegated</span> <span class="pl-c1">=</span> <span class="pl-k">void</span> <span class="pl-c1">0</span><span class="pl-kos">;</span>  
                <span class="pl-s1">instruction</span><span class="pl-kos">[</span><span class="pl-c1">0</span><span class="pl-kos">]</span> <span class="pl-c1">=</span> <span class="pl-c1">1</span> <span class="pl-c">/*throw*/</span><span class="pl-kos">,</span> <span class="pl-s1">instruction</span><span class="pl-kos">[</span><span class="pl-c1">1</span><span class="pl-kos">]</span> <span class="pl-c1">=</span> <span class="pl-s1">e</span><span class="pl-kos">;</span>  
            <span class="pl-kos">}</span>  
        <span class="pl-kos">}</span>  
    <span class="pl-kos">}</span>  
    <span class="pl-k">return</span> <span class="pl-kos">{</span>  
        <span class="pl-en">next</span>: <span class="pl-k">function</span> <span class="pl-kos">(</span><span class="pl-s1">v</span><span class="pl-kos">)</span> <span class="pl-kos">{</span> <span class="pl-k">return</span> <span class="pl-en">step</span><span class="pl-kos">(</span><span class="pl-kos">[</span><span class="pl-c1">0</span> <span class="pl-c">/*next*/</span><span class="pl-kos">,</span> <span class="pl-s1">v</span><span class="pl-kos">]</span><span class="pl-kos">)</span><span class="pl-kos">;</span> <span class="pl-kos">}</span><span class="pl-kos">,</span>  
        <span class="pl-s">"throw"</span>: <span class="pl-k">function</span> <span class="pl-kos">(</span><span class="pl-s1">v</span><span class="pl-kos">)</span> <span class="pl-kos">{</span> <span class="pl-k">return</span> <span class="pl-en">step</span><span class="pl-kos">(</span><span class="pl-kos">[</span><span class="pl-c1">1</span> <span class="pl-c">/*throw*/</span><span class="pl-kos">,</span> <span class="pl-s1">v</span><span class="pl-kos">]</span><span class="pl-kos">)</span><span class="pl-kos">;</span> <span class="pl-kos">}</span><span class="pl-kos">,</span>  
        <span class="pl-s">"return"</span>: <span class="pl-k">function</span> <span class="pl-kos">(</span><span class="pl-s1">v</span><span class="pl-kos">)</span> <span class="pl-kos">{</span> <span class="pl-k">return</span> <span class="pl-en">step</span><span class="pl-kos">(</span><span class="pl-kos">[</span><span class="pl-c1">2</span> <span class="pl-c">/*return*/</span><span class="pl-kos">,</span> <span class="pl-s1">v</span><span class="pl-kos">]</span><span class="pl-kos">)</span><span class="pl-kos">;</span> <span class="pl-kos">}</span><span class="pl-kos">,</span>  
    <span class="pl-kos">}</span><span class="pl-kos">;</span>  
<span class="pl-kos">}</span><span class="pl-kos">;</span></pre></div>