Skip to content

Commit 034aed5

Browse files
authored
Clarify generator delegation (fixes nzakas#377)
1 parent 6a76cb8 commit 034aed5

File tree

1 file changed

+2
-2
lines changed

1 file changed

+2
-2
lines changed

manuscript/08-Iterators-And-Generators.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -726,7 +726,7 @@ I> The spread operator and `for-of` ignore any value specified by a `return` sta
726726

727727
### Delegating Generators
728728

729-
In some cases, combining the values from two iterators into one is useful. Generators can delegate to other generators using a special form of `yield` with a star (`*`) character. As with generator definitions, where the star appears doesn't matter, as long as the star falls between the `yield` keyword and the generator function name. Here's an example:
729+
In some cases, combining the values from two iterators into one is useful. Generators can delegate to other iterators using a special form of `yield` with a star (`*`) character. As with generator definitions, where the star appears doesn't matter, as long as the star falls between the `yield` keyword and the generator function name. Here's an example:
730730

731731
```js
732732
function *createNumberIterator() {
@@ -755,7 +755,7 @@ console.log(iterator.next()); // "{ value: true, done: false }"
755755
console.log(iterator.next()); // "{ value: undefined, done: true }"
756756
```
757757

758-
In this example, the `createCombinedIterator()` generator delegates first to `createNumberIterator()` and then to `createColorIterator()`. The returned iterator appears, from the outside, to be one consistent iterator that has produced all of the values. Each call to `next()` is delegated to the appropriate iterator until the iterators created by `createNumberIterator()` and `createColorIterator()` are empty. Then the final `yield` is executed to return `true`.
758+
In this example, the `createCombinedIterator()` generator delegates first to iterator returned from `createNumberIterator()` and then to the iterator returned from `createColorIterator()`. The iterator returned from `createCombinedIterator()` appears, from the outside, to be one consistent iterator that has produced all of the values. Each call to `next()` is delegated to the appropriate iterator until the iterators created by `createNumberIterator()` and `createColorIterator()` are empty. Then the final `yield` is executed to return `true`.
759759

760760
Generator delegation also lets you make further use of generator return values. This is the easiest way to access such returned values and can be quite useful in performing complex tasks. For example:
761761

0 commit comments

Comments
 (0)