The Generator object is returned by a generator function and it conforms to both the iterable protocol and the iterator protocol.
Constructor
This object cannot be instantiated directly. Instead, a Generator instance can be returned from a generator function:
function* generator() {
yield 1;
yield 2;
yield 3;
}
const gen = generator(); // "Generator { }"
Instance methods
Generator.prototype.next()- Returns a value yielded by the
yieldexpression. 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).
Examples
An infinite iterator
function* infinite() {
let index = 0;
while (true) {
yield index++;
}
}
const generator = infinite(); // "Generator { }"
console.log(generator.next().value); // 0
console.log(generator.next().value); // 1
console.log(generator.next().value); // 2
// ...
Specifications
| Specification |
|---|
| ECMAScript (ECMA-262) The definition of 'Generator objects' in that specification. |
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.
| Desktop | Mobile | Server | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Generator | Chrome Full support 39 | Edge Full support 13 | Firefox Full support 26 | IE No support No | Opera Full support 26 | Safari Full support 10 | WebView Android Full support 39 | Chrome Android Full support 39 | Firefox Android Full support 26 | Opera Android Full support 26 | Safari iOS Full support 10 | Samsung Internet Android Full support 4.0 | 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 26 | Safari Full support 10 | WebView Android Full support 39 | Chrome Android Full support 39 | Firefox Android Full support 26 | Opera Android Full support 26 | Safari iOS Full support 10 | Samsung Internet Android Full support 4.0 | 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 37 | 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 26 | Safari Full support 10 | WebView Android Full support 39 | Chrome Android Full support 39 | Firefox Android Full support 26 | Opera Android Full support 26 | Safari iOS Full support 10 | Samsung Internet Android Full support 4.0 | 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.
