{{JSRef("Global_Objects", "Generator")}} {{harmony}}
Summary
The Generator
object is returned by a {{jsxref("Statements/function*", "generator function", "", 1)}} and it conforms to both the iterator and the Iterable protocol.
Syntax
function* gen() { yield 1; yield 2; yield 3; } var g = gen(); // "Generator { }"
Methods
- {{jsxref("Generator.prototype.next()")}}
- Returns a value yielded by the {{jsxref("Operators/yield", "yield")}} expression.
- {{jsxref("Generator.prototype.return()")}}
- Returns the given value and finishes the generator.
- {{jsxref("Generator.prototype.throw()")}}
- Throws an error to a generator.
Example
An infinite iterator
function* idMaker(){ var index = 0; while(true) yield index++; } var gen = idMaker(); // "Generator { }" console.log(gen.next().value); // 0 console.log(gen.next().value); // 1 console.log(gen.next().value); // 2 // ...
Legacy generator objects
Firefox (SpiderMonkey) also implements an earlier version of generators in JavaScript 1.7, where the star (*) in the function declaration was not necessary (you just use the yield
keyword in the function body). However, legacy generators are deprecated. Do not use them; they are going to be removed ({{bug(1083482)}}).
Legacy generator methods
Generator.prototype.next()
{{non-standard_inline}}- Returns a value yielded by the {{jsxref("Operators/yield", "yield")}} expression. This corresponds to
next()
in the ES6 generator object. Generator.prototype.close()
{{non-standard_inline}}- Closes the generator, so that when calling
next()
an {{jsxref("StopIteration")}} error will be thrown. This corresponds to thereturn()
method in the ES6 generator object. Generator.prototype.send()
{{non-standard_inline}}- Used to send a value to a generator. The value is returned from the {{jsxref("Operators/yield", "yield")}} expression, and returns a value yielded by the next {{jsxref("Operators/yield", "yield")}} expression.
send(x)
corresponds tonext(x)
in the ES6 generator object. Generator.
prototype.
throw()
{{non-standard_inline}}- Throws an error to a generator. This corresponds to the
throw()
method in the ES6 generator object.
Legacy generator example
function fibonacci() { var a = yield 1; yield a * 2; } var it = fibonacci(); console.log(it); // "Generator { }" console.log(it.next()); // 1 console.log(it.send(10)); // 20 console.log(it.close()); // undefined console.log(it.next()); // throws StopIteration (as the generator is now closed)
Specifications
Specification | Status | Comment |
---|---|---|
{{SpecName('ES6', '#sec-generator-objects', 'Generator objects')}} | {{Spec2('ES6')}} | Initial definition. |
Browser compatibility
{{CompatibilityTable}}
Feature | Chrome | Firefox (Gecko) | Internet Explorer | Opera | Safari |
---|---|---|---|---|---|
Basic support | {{CompatNo}} | {{CompatVersionUnknown}} | {{CompatNo}} | {{CompatNo}} | {{CompatNo}} |
Feature | Android | Chrome for Android | Firefox Mobile (Gecko) | IE Mobile | Opera Mobile | Safari Mobile |
---|---|---|---|---|---|---|
Basic support | {{CompatNo}} | {{CompatNo}} | {{CompatVersionUnknown}} | {{CompatNo}} | {{CompatNo}} | {{CompatNo}} |
See also
Legacy generators
- {{jsxref("Statements/Legacy_generator_function", "The legacy generator function", "", 1)}}
- {{jsxref("Operators/Legacy_generator_function", "The legacy generator function expression", "", 1)}}
- {{jsxref("StopIteration")}}
- The legacy Iterator protocol
ES6 generators
- {{jsxref("Functions", "Functions", "", 1)}}
- {{jsxref("Statements/function", "function")}}
- {{jsxref("Operators/function", "function expression")}}
- {{jsxref("Function")}}
- {{jsxref("Statements/function*", "function*")}}
- {{jsxref("Operators/function*", "function* expression")}}
- {{jsxref("GeneratorFunction")}}
- The Iterator protocol