mozilla

Revision 532155 of function*

  • Revision slug: Web/JavaScript/Reference/Statements/function*
  • Revision title: Generators
  • Revision id: 532155
  • Created:
  • Creator: dbruant
  • Is current revision? No
  • Comment

Revision Content

{{Harmony}}

Summary

Generators functions enable writing iterators more easily.

Expression
Implemented in: Firefox 26 (SpiderMonkey 26)
ECMA Version: ECMAScript 6th Edition (Draft)

Syntax

function* gen(i){
  while(true){
    yield i++;  
  }
}

Description

Generators are functions which can be exited and later re-entered. Their context (variable bindings) will be saved across re-entrances.

Examples

Simple example

function* idMaker(){
    var index = 0;
    while(true)
        yield index++;
}

var gen = idMaker();

console.log(gen.next().value); // 0
console.log(gen.next().value); // 1
console.log(gen.next().value); // 2
// ...

 

Specification

  • Harmony proposal
  • Draft spec

Browser compatibility

{{CompatibilityTable}}
Feature Chrome Firefox (Gecko) Internet Explorer Opera Safari (WebKit)
Basic support yes (when?) {{CompatGeckoDesktop("26.0")}} {{CompatNo}} {{CompatNo}} {{CompatNo}}
Feature Android Firefox Mobile (Gecko) IE Phone Opera Mobile Safari Mobile
Basic support yes (when?) {{CompatGeckoMobile("27.0")}} {{CompatNo}} {{CompatNo}} {{CompatNo}}

 

See also

Revision Source

<p>{{Harmony}}</p>
<h2 id="Summary" name="Summary">Summary</h2>
<p>Generators functions enable writing <a href="/en-US/docs/Web/JavaScript/Guide/The_Iterator_protocol">iterators</a> more easily.</p>
<table class="standard-table">
 <tbody>
  <tr>
   <td class="header" colspan="2">Expression</td>
  </tr>
  <tr>
   <td>Implemented in:</td>
   <td>Firefox 26 (SpiderMonkey 26)</td>
  </tr>
  <tr>
   <td>ECMA Version:</td>
   <td>ECMAScript 6th Edition (Draft)</td>
  </tr>
 </tbody>
</table>
<h2 id="Syntax" name="Syntax">Syntax</h2>
<pre class="brush: js">
function* gen(i){
  while(true){
    yield i++;  
  }
}
</pre>
<h2 id="Description">Description</h2>
<p>Generators are functions which can be exited and later re-entered. Their context (variable bindings) will be saved across re-entrances.</p>
<h2 id="Examples">Examples</h2>
<h3>Simple example</h3>
<pre>
<code class="brush: js"><span>function</span><span class="token operator">*</span> <span class="token function">idMaker<span class="token punctuation">(</span></span><span class="token punctuation">)</span><span class="token punctuation">{</span>
    <span class="token keyword">var</span> index <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span>
    <span class="token keyword">while</span><span class="token punctuation">(</span><span class="token boolean">true</span><span class="token punctuation">)</span>
        yield index<span class="token operator">++</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>

<span class="token keyword">var</span> gen <span class="token operator">=</span> <span class="token function">idMaker<span class="token punctuation">(</span></span><span class="token punctuation">)</span><span class="token punctuation">;</span>

console<span class="token punctuation">.</span><span class="token function">log<span class="token punctuation">(</span></span>gen<span class="token punctuation">.</span><span class="token function">next<span class="token punctuation">(</span></span><span class="token punctuation">)</span><span class="token punctuation">.</span>value<span class="token punctuation">)</span><span class="token punctuation">;</span> // 0
console<span class="token punctuation">.</span><span class="token function">log<span class="token punctuation">(</span></span>gen<span class="token punctuation">.</span><span class="token function">next<span class="token punctuation">(</span></span><span class="token punctuation">)</span><span class="token punctuation">.</span>value<span class="token punctuation">)</span><span class="token punctuation">; // 1
</span>console<span class="token punctuation">.</span><span class="token function">log<span class="token punctuation">(</span></span>gen<span class="token punctuation">.</span><span class="token function">next<span class="token punctuation">(</span></span><span class="token punctuation">)</span><span class="token punctuation">.</span>value<span class="token punctuation">)</span><span class="token punctuation">; // 2
// ...</span></code></pre>
<p>&nbsp;</p>
<h2 id="Specification">Specification</h2>
<ul>
 <li>Harmony proposal</li>
 <li>Draft spec</li>
</ul>
<h2 id="Browser_compatibility" name="Browser_compatibility">Browser compatibility</h2>
<div>
 {{CompatibilityTable}}</div>
<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>yes (when?)</td>
    <td>{{CompatGeckoDesktop("26.0")}}</td>
    <td>{{CompatNo}}</td>
    <td>{{CompatNo}}</td>
    <td>{{CompatNo}}</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 Phone</th>
    <th>Opera Mobile</th>
    <th>Safari Mobile</th>
   </tr>
   <tr>
    <td>Basic support</td>
    <td>yes (when?)</td>
    <td>{{CompatGeckoMobile("27.0")}}</td>
    <td>{{CompatNo}}</td>
    <td>{{CompatNo}}</td>
    <td>{{CompatNo}}</td>
   </tr>
  </tbody>
 </table>
</div>
<p>&nbsp;</p>
<h2 id="See_also">See also</h2>
<ul>
 <li><a href="http://facebook.github.io/regenerator/">Regenerator</a> an ES6 generator compiler to ES5</li>
 <li><a href="http://www.youtube.com/watch?v=qbKWsbJ76-s">Forbes Lindesay: Promises and Generators: control flow utopia -- JSConf EU 2013</a></li>
 <li><a href="http://taskjs.org/">Task.js</a></li>
</ul>
Revert to this revision