Please note, this is a STATIC archive of website developer.mozilla.org from 03 Nov 2016, cach3.com does not collect or store any user information, there is no "phishing" involved.

Revision 945061 of yield

  • Revision slug: Web/JavaScript/Reference/Operators/yield
  • Revision title: yield
  • Revision id: 945061
  • Created:
  • Creator: fscholz
  • Is current revision? No
  • Comment add ES draft

Revision Content

{{jsSidebar("Operators")}}

The yield keyword is used to pause and resume a generator function ({{jsxref("Statements/function*", "function*")}} or legacy generator function).

Syntax

[rv] = yield [expression];
expression
Defines the value to return from the generator function via the iterator protocol. If omitted, undefined is returned instead.
rv
Lets the generator capture the value of expression for use the next time it resumes execution.

Description

The yield keyword causes generator function execution to pause and the value of the expression following the yield keyword is returned to the generator's caller. It can be thought of as a generator-based version of the return keyword.

The yield keyword actually returns an IteratorResult object with two properties, value and done. The value property is the result of evaluating the yield expression, and done is a Boolean indicating whether or not the generator function has fully completed.

Once paused on a yield expression, the generator's code execution remains paused until the generator's next() method is called. Each time the generator's next() method is called, the generator resumes execution and runs until it reaches one of the following:

  •  A yield, which causes the generator to once again pause and return the generator's new value. The next time next() is called, execution resumes with the statement immediately after the yield.
  • {{jsxref("Statements/throw", "throw")}} is used to throw an exception from the generator. This halts execution of the generator entirely, and execution resumes in the caller as is normally the case when an exception is thrown.
  • The end of the generator function is reached; in this case, execution of the generator ends and an IteratorResult is returned to the caller in which the value is {{jsxref("undefined")}} and done is true.
  • A {{jsxref("Statements/return", "return")}} statement is reached. In this case, execution of the generator ends and an IteratorResult is returned to the caller in which the value is the value specified by the return statement and done is true.

If an optional value is passed to the generator's next() method, that value becomes the value returned by the generator's next yield operation.

Between the generator's code path, its yield operators, and the ability to specify a new starting value by passing it to {{jsxref("Generator.prototype.next()")}}, generators offer enormous power and control.

Examples

The following code is the declaration of an example generator function, along with a helper function.

function* foo(){
  var index = 0;
  while (index <= 2) // when index reaches 3, 
                     // yield's done will be true 
                     // and its value will be undefined;
    yield index++;
}

Once a generator function is defined, it can be used by constructing an iterator as shown.

var iterator = foo();
console.log(iterator.next()); // { value: 0, done: false }
console.log(iterator.next()); // { value: 1, done: false }
console.log(iterator.next()); // { value: 2, done: false }
console.log(iterator.next()); // { value: undefined, done: true }

Specifications

Specification Status Comment
{{SpecName('ES6', '#', 'Yield')}} {{Spec2('ES6')}} Initial definition.
{{SpecName('ESDraft', '#', 'Yield')}} {{Spec2('ESDraft')}}  

Browser compatibility

{{CompatibilityTable}}

Feature Chrome Firefox (Gecko) Internet Explorer Opera Safari (WebKit)
Basic support 39 {{CompatGeckoDesktop("26.0")}} {{CompatUnknown}} {{CompatUnknown}} {{CompatUnknown}}
IteratorResult object instead of throwing {{CompatUnknown}} {{CompatGeckoDesktop("29.0")}} {{CompatUnknown}} {{CompatUnknown}} {{CompatUnknown}}
Feature Android Firefox Mobile (Gecko) IE Mobile Opera Mobile Safari Mobile
Basic support {{CompatVersionUnknown}} {{CompatGeckoMobile("26.0")}} {{CompatUnknown}} {{ CompatUnknown}} {{CompatUnknown}}
IteratorResult object instead of throwing {{CompatUnknown}} {{CompatGeckoMobile("29.0")}} {{CompatUnknown}} {{CompatUnknown}} {{CompatUnknown}}

Firefox-specific notes

  • Starting with Gecko 29 {{geckoRelease(29)}}, the completed generator function no longer throws a {{jsxref("TypeError")}} "generator has already finished". Instead, it returns an IteratorResult object like { value: undefined, done: true } ({{bug(958951)}}).
  • Starting with Gecko 33 {{geckoRelease(33)}}, the parsing of the yield expression has been updated to conform with the latest ES6 specification ({{bug(981599)}}):
    • The expression after the yield keyword is optional and omitting it no longer throws a {{jsxref("SyntaxError")}}: function* foo() { yield; }

See also

  • The Iterator protocol
  • {{jsxref("Statements/function*", "function*")}}
  • {{jsxref("Operators/function*", "function* expression")}}
  • {{jsxref("Operators/yield*", "yield*")}}

Revision Source

<div>{{jsSidebar("Operators")}}</div>

<p>The <code>yield</code> keyword is used to pause and resume a generator function ({{jsxref("Statements/function*", "function*")}} or <a href="/en-US/docs/Web/JavaScript/Reference/Statements/Legacy_generator_function">legacy generator function</a>).</p>

<h2 id="Syntax">Syntax</h2>

<pre class="syntaxbox">
[<em>rv</em>] = <strong>yield</strong> [<em>expression</em>];</pre>

<dl>
 <dt><code>expression</code></dt>
 <dd>Defines the value to return from the generator function via <a href="/en-US/docs/Web/JavaScript/Reference/Iteration_protocols#iterator">the iterator protocol</a>. If omitted, <code>undefined</code> is returned instead.</dd>
 <dt><code>rv</code></dt>
 <dd>Lets the generator capture the value of <code>expression</code> for use the next time it resumes execution.</dd>
</dl>

<h2 id="Description">Description</h2>

<p>The <code>yield</code> keyword causes generator function execution to pause and the value of the expression following the <code>yield</code> keyword is returned to the generator's caller. It can be thought of as a generator-based version of the <code>return</code> keyword.</p>

<p>The <code>yield</code> keyword actually returns an <code>IteratorResult</code> object with two properties, <code>value</code> and <code>done</code>. The <code>value</code> property is the result of evaluating the <code>yield</code> expression, and <code>done</code> is a Boolean indicating whether or not the generator function has fully completed.</p>

<p>Once paused on a <code>yield</code> expression, the generator's code execution remains paused until the generator's <code>next()</code> method is called. Each time the generator's <code>next()</code> method is called, the generator resumes execution and runs until it reaches one of the following:</p>

<ul>
 <li>&nbsp;A <code>yield</code>, which causes the generator to once again pause and return the generator's new value. The next time <code>next()</code> is called, execution resumes with the statement immediately after the <code>yield</code>.</li>
 <li>{{jsxref("Statements/throw", "throw")}} is used to throw an exception from the generator. This halts execution of the generator entirely, and execution resumes in the caller as is normally the case when an exception is thrown.</li>
 <li>The end of the generator function is reached; in this case, execution of the generator ends and an <code>IteratorResult</code> is returned to the caller in which the <code>value</code> is {{jsxref("undefined")}} and <code>done</code> is <code>true</code>.</li>
 <li>A {{jsxref("Statements/return", "return")}} statement is reached. In this case, execution of the generator ends and an <code>IteratorResult</code> is returned to the caller in which the <code>value</code> is the value specified by the <code>return</code> statement and <code>done</code> is <code>true</code>.</li>
</ul>

<p>If an optional value is passed to the generator's <code>next()</code> method, that value becomes the value returned by the generator's next <code>yield</code> operation.</p>

<p>Between the generator's code path, its <code>yield</code> operators, and the ability to specify a new starting value by passing it to {{jsxref("Generator.prototype.next()")}}, generators offer enormous power and control.</p>

<h2 id="Examples">Examples</h2>

<p>The following code is the declaration of an example generator function, along with a helper function.</p>

<pre class="brush: js">
function* foo(){
  var index = 0;
  while (index &lt;= 2) // when index reaches 3, 
                     // yield's done will be true 
                     // and its value will be undefined;
    yield index++;
}</pre>

<p>Once a generator function is defined, it can be used by constructing an iterator as shown.</p>

<pre class="brush: js">
var iterator = foo();
console.log(iterator.next()); // { value: 0, done: false }
console.log(iterator.next()); // { value: 1, done: false }
console.log(iterator.next()); // { value: 2, done: false }
console.log(iterator.next()); // { value: undefined, done: true }</pre>

<h2 id="Specifications">Specifications</h2>

<table class="standard-table">
 <thead>
  <tr>
   <th scope="col">Specification</th>
   <th scope="col">Status</th>
   <th scope="col">Comment</th>
  </tr>
 </thead>
 <tbody>
  <tr>
   <td>{{SpecName('ES6', '#', 'Yield')}}</td>
   <td>{{Spec2('ES6')}}</td>
   <td>Initial definition.</td>
  </tr>
  <tr>
   <td>{{SpecName('ESDraft', '#', 'Yield')}}</td>
   <td>{{Spec2('ESDraft')}}</td>
   <td>&nbsp;</td>
  </tr>
 </tbody>
</table>

<h2 id="Browser_compatibility">Browser compatibility</h2>

<p>{{CompatibilityTable}}</p>

<div id="compat-desktop">
<table class="compat-table">
 <tbody>
  <tr>
   <th>Feature</th>
   <th>Chrome</th>
   <th>Firefox (Gecko)</th>
   <th>Internet Explorer</th>
   <th>Opera</th>
   <th>Safari (WebKit)</th>
  </tr>
  <tr>
   <td>Basic support</td>
   <td>39</td>
   <td>{{CompatGeckoDesktop("26.0")}}</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatUnknown}}</td>
  </tr>
  <tr>
   <td><code>IteratorResult</code> object instead of throwing</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatGeckoDesktop("29.0")}}</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatUnknown}}</td>
  </tr>
 </tbody>
</table>
</div>

<div id="compat-mobile">
<table class="compat-table">
 <tbody>
  <tr>
   <th>Feature</th>
   <th>Android</th>
   <th>Firefox Mobile (Gecko)</th>
   <th>IE Mobile</th>
   <th>Opera Mobile</th>
   <th>Safari Mobile</th>
  </tr>
  <tr>
   <td>Basic support</td>
   <td>{{CompatVersionUnknown}}</td>
   <td>{{CompatGeckoMobile("26.0")}}</td>
   <td>{{CompatUnknown}}</td>
   <td>{{ CompatUnknown}}</td>
   <td>{{CompatUnknown}}</td>
  </tr>
  <tr>
   <td><code>IteratorResult</code> object instead of throwing</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatGeckoMobile("29.0")}}</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatUnknown}}</td>
  </tr>
 </tbody>
</table>
</div>

<h2 id="Firefox-specific_notes">Firefox-specific notes</h2>

<ul>
 <li>Starting with Gecko 29 {{geckoRelease(29)}}, the completed generator function no longer throws a {{jsxref("TypeError")}} "generator has already finished". Instead, it returns an <code>IteratorResult</code> object like <code>{ value: undefined, done: true }</code> ({{bug(958951)}}).</li>
 <li>Starting with Gecko 33 {{geckoRelease(33)}}, the parsing of the <code>yield</code> expression has been updated to conform with the latest ES6 specification ({{bug(981599)}}):
  <ul>
   <li>The expression after the <code>yield</code> keyword is optional and omitting it no longer throws a {{jsxref("SyntaxError")}}: <code>function* foo() { yield; }</code></li>
  </ul>
 </li>
</ul>

<h2 id="See_also">See also</h2>

<ul>
 <li><a href="/en-US/docs/Web/JavaScript/Guide/The_Iterator_protocol">The Iterator protocol</a></li>
 <li>{{jsxref("Statements/function*", "function*")}}</li>
 <li>{{jsxref("Operators/function*", "function* expression")}}</li>
 <li>{{jsxref("Operators/yield*", "yield*")}}</li>
</ul>
Revert to this revision