Generators
Generator functions (function*) create iterators that return multiple values, one after another, on-demand.
Overview
Generator functions (function*) create iterators that return multiple values, one after another, on-demand.
Browser support
| Feature | Desktop | Mobile | ||||
|---|---|---|---|---|---|---|
| Chrome | Edge | Firefox | Safari | Chrome Android | Safari iOS | |
| 39 | 13 | 26 | 10 | 39 | 10 | |
| The next() method of Generator instances returns an object with two properties done and value. You can also provide a parameter to the next method to send a value to the generator. | 39 | 13 | 26 | 10 | 39 | 10 |
| The return() method of Generator instances acts as if a return statement is inserted in the generator's body at the current suspended position, which finishes the generator and allows the generator to perform any cleanup tasks when combined with a try...finally block. | 50 | 13 | 38 | 10 | 50 | 10 |
| The throw() method of Generator instances acts as if a throw statement is inserted in the generator's body at the current suspended position, which informs the generator of an error condition and allows it to handle the error, or perform cleanup and close itself. | 39 | 13 | 26 | 10 | 39 | 10 |
| Other | ||||||
| The GeneratorFunction object provides methods for generator functions. In JavaScript, every generator function is actually a GeneratorFunction object. | 39 | 13 | 26 | 10 | 39 | 10 |
| Built-in object | ||||||
| The GeneratorFunction() constructor creates GeneratorFunction objects. | 39 | 13 | 26 | 10 | 39 | 10 |
| Other | ||||||
| `function*` expression | 49 | 12 | 26 | 10 | 49 | 10 |
| Operator | ||||||
trailing comma Trailing comma in parameters | 58 | 79 | 52 | 10 | 58 | 10 |
| Other | ||||||
| The yield operator is used to pause and resume a generator function. | 39 | 12 | 26 | 10 | 39 | 10 |
| `yield*` | 39 | 12 | 27 | 10 | 39 | 10 |
| `function*` statement | 39 | 13 | 26 | 10 | 39 | 10 |
| Statement | ||||||
IteratorResult object `IteratorResult` object instead of throwing | 49 | 13 | 29 | 10 | 49 | 10 |
not constructable with new Not constructable with `new` (ES2016) | 50 | 13 | 43 | 10 | 50 | 10 |
trailing comma in parameters Trailing comma in parameters | 58 | 14 | 52 | 10 | 58 | 10 |
- Starting with Firefox 33, the parsing of the `yield` expression has been updated to conform with the ES2015 specification.
- Starting with Firefox 29, an `IteratorResult` object returned for completed generator function.
- Starting with Firefox 33, the parsing of the `yield` expression has been updated to conform with the ES2015 specification.
Syntax
function* fibonacci() {
let [a, b] = [0, 1];
while (true) {
yield a;
[a, b] = [b, a + b];
}
}
const fib = fibonacci();
fib.next().value; // 0
fib.next().value; // 1
fib.next().value; // 1
fib.next().value; // 2 Live demo
steprungenerator
Processing one timeStop, one timebetween per and to next. Value generatesimul-tion.. with yield
Use cases
Using Generators
Generator functions (function*) create iterators that return multiple values, one after another, on-demand.
Cautions
- No specific concerns. Stable across all major browsers.
Accessibility
- When updating the DOM dynamically, announce important changes to assistive technology with aria-live regions.
Related links
Powered by web-features