The Generator
object is returned by a generator function and it conforms to both the iterable protocol and the iterator protocol.
function* gen() { yield 1; yield 2; yield 3; } var g = gen(); // "Generator { }"
Generator.prototype.next()
yield
expression.Generator.prototype.return()
Generator.prototype.throw()
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 // ...
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).
Generator.prototype.next()
yield
expression. This corresponds to next()
in the ES2015 generator object.Generator.prototype.close()
next()
an StopIteration
error will be thrown. This corresponds to the return()
method in the ES2015 generator object.Generator.prototype.send()
yield
expression, and returns a value yielded by the next yield
expression. send(x)
corresponds to next(x)
in the ES2015 generator object.Generator.
prototype.
throw()
throw()
method in the ES2015 generator object.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)
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 |
Desktop | ||||||
---|---|---|---|---|---|---|
Chrome | Edge | Firefox | Internet Explorer | Opera | Safari | |
Basic support | 39 | 13 | 26 | No | Yes | 10 |
next |
39 | 13 | 26 | No | Yes | 10 |
return |
50 | 13 | 38 | No | 37 | 10 |
throw |
39 | 13 | 26 | No | Yes | 10 |
Mobile | |||||||
---|---|---|---|---|---|---|---|
Android webview | Chrome for Android | Edge Mobile | Firefox for Android | Opera for Android | iOS Safari | Samsung Internet | |
Basic support | Yes | Yes | 13 | 26 | Yes | 10 | Yes |
next |
Yes | Yes | 13 | 26 | Yes | 10 | Yes |
return |
? | 50 | 13 | 38 | Yes | 10 | 5.0 |
throw |
Yes | Yes | 13 | 26 | Yes | 10 | Yes |
Server | |
---|---|
Node.js | |
Basic support | 4.0.0
|
next |
Yes |
return |
6.0.0 |
throw |
4.0.0
|
StopIteration
function
function expression
Function
function*
function* expression
GeneratorFunction
© 2005–2018 Mozilla Developer Network and individual contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Generator