Skip to content

Commit cb8ca5c

Browse files
authored
Merge pull request #1574 from hrodward/patch-22
Update article.md
2 parents dcc7e48 + 8dfb5e8 commit cb8ca5c

File tree

1 file changed

+11
-11
lines changed
  • 1-js/12-generators-iterators/1-generators

1 file changed

+11
-11
lines changed

1-js/12-generators-iterators/1-generators/article.md

+11-11
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ The function code execution hasn't started yet:
4040

4141
![](generateSequence-1.svg)
4242

43-
The main method of a generator is `next()`. When called, it runs the execution till the nearest `yield <value>` statement (`value` can be omitted, then it's `undefined`). Then the function execution pauses, and the yielded `value` is returned to the outer code.
43+
The main method of a generator is `next()`. When called, it runs the execution until the nearest `yield <value>` statement (`value` can be omitted, then it's `undefined`). Then the function execution pauses, and the yielded `value` is returned to the outer code.
4444

4545
The result of `next()` is always an object with two properties:
4646
- `value`: the yielded value.
@@ -78,7 +78,7 @@ alert(JSON.stringify(two)); // {value: 2, done: false}
7878

7979
![](generateSequence-3.svg)
8080

81-
And, if we call it the third time, then the execution reaches `return` statement that finishes the function:
81+
And, if we call it a third time, the execution reaches the `return` statement that finishes the function:
8282

8383
```js
8484
let three = generator.next();
@@ -90,7 +90,7 @@ alert(JSON.stringify(three)); // {value: 3, *!*done: true*/!*}
9090

9191
Now the generator is done. We should see it from `done:true` and process `value:3` as the final result.
9292

93-
New calls `generator.next()` don't make sense any more. If we do them, they return the same object: `{done: true}`.
93+
New calls to `generator.next()` don't make sense any more. If we do them, they return the same object: `{done: true}`.
9494

9595
```smart header="`function* f(…)` or `function *f(…)`?"
9696
Both syntaxes are correct.
@@ -154,7 +154,7 @@ let sequence = [0, ...generateSequence()];
154154
alert(sequence); // 0, 1, 2, 3
155155
```
156156

157-
In the code above, `...generateSequence()` turns the iterable generator object into array of items (read more about the spread operator in the chapter [](info:rest-parameters-spread-operator#spread-operator))
157+
In the code above, `...generateSequence()` turns the iterable generator object into an array of items (read more about the spread operator in the chapter [](info:rest-parameters-spread-operator#spread-operator))
158158

159159
## Using generators for iterables
160160

@@ -212,17 +212,17 @@ alert( [...range] ); // 1,2,3,4,5
212212
```
213213

214214
That works, because `range[Symbol.iterator]()` now returns a generator, and generator methods are exactly what `for..of` expects:
215-
- it has `.next()` method
215+
- it has a `.next()` method
216216
- that returns values in the form `{value: ..., done: true/false}`
217217

218-
That's not a coincidence, of course. Generators were added to JavaScript language with iterators in mind, to implement them easier.
218+
That's not a coincidence, of course. Generators were added to JavaScript language with iterators in mind, to implement them easily.
219219

220220
The variant with a generator is much more concise than the original iterable code of `range`, and keeps the same functionality.
221221

222222
```smart header="Generators may generate values forever"
223223
In the examples above we generated finite sequences, but we can also make a generator that yields values forever. For instance, an unending sequence of pseudo-random numbers.
224224
225-
That surely would require a `break` (or `return`) in `for..of` over such generator, otherwise the loop would repeat forever and hang.
225+
That surely would require a `break` (or `return`) in `for..of` over such generator. Otherwise, the loop would repeat forever and hang.
226226
```
227227

228228
## Generator composition
@@ -237,7 +237,7 @@ function* generateSequence(start, end) {
237237
}
238238
```
239239

240-
Now we'd like to reuse it for generation of a more complex sequence:
240+
Now we'd like to reuse it to generate a more complex sequence:
241241
- first, digits `0..9` (with character codes 48..57),
242242
- followed by uppercase alphabet letters `A..Z` (character codes 65..90)
243243
- followed by lowercase alphabet letters `a..z` (character codes 97..122)
@@ -316,7 +316,7 @@ A generator composition is a natural way to insert a flow of one generator into
316316

317317
## "yield" is a two-way road
318318

319-
Till this moment, generators were similar to iterable objects, with a special syntax to generate values. But in fact they are much more powerful and flexible.
319+
Until this moment, generators were similar to iterable objects, with a special syntax to generate values. But in fact they are much more powerful and flexible.
320320

321321
That's because `yield` is a two-way road: it not only returns the result outside, but also can pass the value inside the generator.
322322

@@ -422,7 +422,7 @@ generator.throw(new Error("The answer is not found in my database")); // (2)
422422
*/!*
423423
```
424424

425-
The error, thrown into the generator at the line `(2)` leads to an exception in the line `(1)` with `yield`. In the example above, `try..catch` catches it and shows.
425+
The error, thrown into the generator at line `(2)` leads to an exception in line `(1)` with `yield`. In the example above, `try..catch` catches it and shows it.
426426

427427
If we don't catch it, then just like any exception, it "falls out" the generator into the calling code.
428428

@@ -456,6 +456,6 @@ If we don't catch the error there, then, as usual, it falls through to the outer
456456

457457
In modern JavaScript, generators are rarely used. But sometimes they come in handy, because the ability of a function to exchange data with the calling code during the execution is quite unique. And, surely, they are great for making iterable objects.
458458

459-
Also, in the next chapter we'll learn async generators, which are used to read streams of asynchronously generated data (e.g paginated fetches over a network) in `for await ... of` loop.
459+
Also, in the next chapter we'll learn async generators, which are used to read streams of asynchronously generated data (e.g paginated fetches over a network) in `for await ... of` loops.
460460

461461
In web-programming we often work with streamed data, so that's another very important use case.

0 commit comments

Comments
 (0)