|
1 | 1 |
|
2 | 2 | # Async iterators and generators
|
3 | 3 |
|
4 |
| -Asynchronous iterators allow to iterate over data that comes asynchronously, on-demand. For instance, when we download something chunk-by-chunk over a network. Asynchronous generators make it even more convenient. |
| 4 | +Asynchronous iterators allow us to iterate over data that comes asynchronously, on-demand. Like, for instance, when we download something chunk-by-chunk over a network. And asynchronous generators make it even more convenient. |
5 | 5 |
|
6 | 6 | Let's see a simple example first, to grasp the syntax, and then review a real-life use case.
|
7 | 7 |
|
@@ -52,7 +52,7 @@ If necessary, please refer to the [chapter about iterables](info:iterable) for d
|
52 | 52 | To make the object iterable asynchronously:
|
53 | 53 | 1. We need to use `Symbol.asyncIterator` instead of `Symbol.iterator`.
|
54 | 54 | 2. `next()` should return a promise.
|
55 |
| -3. To iterate over such an object, we should use `for await (let item of iterable)` loop. |
| 55 | +3. To iterate over such an object, we should use a `for await (let item of iterable)` loop. |
56 | 56 |
|
57 | 57 | Let's make an iterable `range` object, like the one before, but now it will return values asynchronously, one per second:
|
58 | 58 |
|
@@ -109,7 +109,7 @@ As we can see, the structure is similar to regular iterators:
|
109 | 109 |
|
110 | 110 | 1. To make an object asynchronously iterable, it must have a method `Symbol.asyncIterator` `(1)`.
|
111 | 111 | 2. This method must return the object with `next()` method returning a promise `(2)`.
|
112 |
| -3. The `next()` method doesn't have to be `async`, it may be a regular method returning a promise, but `async` allows to use `await`, so that's convenient. Here we just delay for a second `(3)`. |
| 112 | +3. The `next()` method doesn't have to be `async`, it may be a regular method returning a promise, but `async` allows us to use `await`, so that's convenient. Here we just delay for a second `(3)`. |
113 | 113 | 4. To iterate, we use `for await(let value of range)` `(4)`, namely add "await" after "for". It calls `range[Symbol.asyncIterator]()` once, and then its `next()` for values.
|
114 | 114 |
|
115 | 115 | Here's a small cheatsheet:
|
@@ -268,7 +268,7 @@ So far we've seen simple examples, to gain basic understanding. Now let's review
|
268 | 268 |
|
269 | 269 | There are many online services that deliver paginated data. For instance, when we need a list of users, a request returns a pre-defined count (e.g. 100 users) - "one page", and provides a URL to the next page.
|
270 | 270 |
|
271 |
| -This pattern is very common. It's not about users, but just about anything. For instance, GitHub allows to retrieve commits in the same, paginated fashion: |
| 271 | +This pattern is very common. It's not about users, but just about anything. For instance, GitHub allows us to retrieve commits in the same, paginated fashion: |
272 | 272 |
|
273 | 273 | - We should make a request to URL in the form `https://api.github.com/repos/<repo>/commits`.
|
274 | 274 | - It responds with a JSON of 30 commits, and also provides a link to the next page in the `Link` header.
|
|
0 commit comments