concept generator in category react

appears as: generators, Generators, generator
Redux in Action

This is an excerpt from Manning's book Redux in Action.

Generators are well-supported by most modern browsers, but that hasn’t always been the case. If your application needs to support older browser versions, consider the impact of including a generator polyfill, if required.

6.4.3. Looping with generators

We’re willing to bet that the last infinite loop you wrote was an accident. It happens to the best of us. In the case of generators however, infinite loops are a viable usage pattern. The next listing is an example of an intentional infinite loop.

It’s also possible to compose generators, or in other words, use generators within generators. In this case (listing 6.5), looping through the count function produces the numbers one through five in order as the count function becomes blocked until middleNumbers resolves. The yield* syntax is used to delegate out to another generator.

Listing 6.5. Composing generators example
function* count() { yield 1; yield 2; yield* middleNumbers(); #1 yield 5; } function* middleNumbers() { yield 3; yield 4; } #1 The middleNumbers generator completes before moving on to 5. !@%STYLE%@! {"css":"{\"css\": \"font-weight: bold;\"}","target":"[[{\"line\":3,\"ch\":36},{\"line\":3,\"ch\":38}],[{\"line\":12,\"ch\":0},{\"line\":12,\"ch\":2}]]"} !@%STYLE%@!