The Generator
object is returned by a generator function and it conforms to both the iterable protocol and the iterator protocol.
Syntax
function* gen() { yield 1; yield 2; yield 3; } var g = gen(); // "Generator { }"
Methods
Generator.prototype.next()
- Returns a value yielded by the
yield
expression. Generator.prototype.return()
- Returns the given value and finishes the generator.
Generator.prototype.throw()
- Throws an error to a generator (also finishes the generator, unless caught from within that 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 implemented 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 support has been removed since Firefox 58 (released on January 23, 2018) (bug 1083482).
Legacy generator methods
Generator.prototype.next()
- Returns a value yielded by the
yield
expression. This corresponds tonext()
in the ES2015 generator object. Generator.prototype.close()
- Closes the generator, so that when calling
next()
anStopIteration
error will be thrown. This corresponds to thereturn()
method in the ES2015 generator object. Generator.prototype.send()
- Used to send a value to a generator. The value is returned from the
yield
expression, and returns a value yielded by the nextyield
expression.send(x)
corresponds tonext(x)
in the ES2015 generator object. Generator.
prototype.
throw()
- Throws an error to a generator. This corresponds to the
throw()
method in the ES2015 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 |
---|---|---|
ECMAScript 2015 (6th Edition, ECMA-262) The definition of 'Generator objects' in that specification. |
Standard | Initial definition. |
ECMAScript Latest Draft (ECMA-262) The definition of 'Generator objects' in that specification. |
Draft |
Browser compatibility
The compatibility table on this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request.
Update compatibility data on GitHub
Desktop | Mobile | Server | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Generator | Chrome Full support 39 | Edge Full support 13 | Firefox Full support 26 | IE No support No | Opera Full support Yes | Safari Full support 10 | WebView Android Full support Yes | Chrome Android Full support 39 | Firefox Android Full support 26 | Opera Android Full support Yes | Safari iOS Full support 10 | Samsung Internet Android Full support Yes | nodejs
Full support
4.0.0
|
next | Chrome Full support 39 | Edge Full support 13 | Firefox Full support 26 | IE No support No | Opera Full support Yes | Safari Full support 10 | WebView Android Full support Yes | Chrome Android Full support 39 | Firefox Android Full support 26 | Opera Android Full support Yes | Safari iOS Full support 10 | Samsung Internet Android Full support Yes | nodejs Full support Yes |
return | Chrome Full support 50 | Edge Full support 13 | Firefox Full support 38 | IE No support No | Opera Full support 37 | Safari Full support 10 | WebView Android Full support 50 | Chrome Android Full support 50 | Firefox Android Full support 38 | Opera Android Full support Yes | Safari iOS Full support 10 | Samsung Internet Android Full support 5.0 | nodejs Full support 6.0.0 |
throw | Chrome Full support 39 | Edge Full support 13 | Firefox Full support 26 | IE No support No | Opera Full support Yes | Safari Full support 10 | WebView Android Full support Yes | Chrome Android Full support 39 | Firefox Android Full support 26 | Opera Android Full support Yes | Safari iOS Full support 10 | Samsung Internet Android Full support Yes | nodejs
Full support
4.0.0
|
Legend
- Full support
- Full support
- No support
- No support
- User must explicitly enable this feature.
- User must explicitly enable this feature.
See also
Legacy generators
- The legacy generator function
- The legacy generator function expression
StopIteration
- The legacy Iterator protocol