Back to Home

Async iteration and generators

Asynchronous iteration 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. Let’s see a simple example first, to grasp the syntax, and then review a real-life use case.

Recall iterables

Let’s recall the topic about iterables. The idea is that we have an object, such as range here: …And we’d like to use for..of loop on it, such as for(value of range), to get values from 1 to 5. In other words, we want to add an iteration ability to the object. That can be implemented using a special method with the name Symbol.iterator: - This method is called in by the for..of construct when the loop is started, and it should return an object with the next method. - For each iteration, the next() method is invoked for the next value. - The next() should return a value in the form {done: true/false, value:}, where done:true means the end of the loop. Here’s an implementation for the iterable range: If anything is unclear, please visit the chapter , it gives all the details about regular iterables.

Async iterables

Asynchronous iteration is needed when values come asynchronously: after setTimeout or another kind of delay. The most common case is that the object needs to make a network request to deliver the next value, we’ll see a real-life example of it a bit later. To make an object iterable asynchronously: 1. Use Symbol.asyncIterator instead of Symbol.iterator. 2. The next() method should return a promise (to be fulfilled with the next value). - The async keyword handles it, we can simply make async next(). 3. To iterate over such an object, we should use a for await (let item of iterable) loop. - Note the await word. As a starting example, let’s make an iterable range object, similar like the one before, but now it will return values asynchronously, one per second. All we need to do is to perform a few replacements in the code above: As we can see, the structure is similar to regular iterators: 1. To make an object asynchronously iterable, it must have a method Symbol.asyncIterator (1). 2. This method must return the object with next() method returning a promise (2). 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). 4. To iterate, we use for await(let value of range) (4), namely add “await” after “for”. It calls rangeSymbol.asyncIterator once, and then its next() for values. Here’s a small table with the differences: alert( […range] ); // Error, no Symbol.iterator

Recall generators

Now let’s recall generators, as they allow to make iteration code much shorter. Most of the time, when we’d like to make an iterable, we’ll use generators. For sheer simplicity, omitting some important stuff, they are “functions that generate (yield) values”. They are explained in detail in the chapter . Generators are labelled with function* (note the star) and use yield to generate a value, then we can use for..of to loop over them. This example generates a sequence of values from start to end: As we already know, to make an object iterable, we should add Symbol.iterator to it. A common practice for Symbol.iterator is to return a generator, it makes the code shorter, as you can see: Please see the chapter if you’d like more details. In regular generators we can’t use await. All values must come synchronously, as required by the for..of construct. What if we’d like to generate values asynchronously? From network requests, for instance. Let’s switch to asynchronous generators to make it possible.

Async generators (finally)

For most practical applications, when we’d like to make an object that asynchronously generates a sequence of values, we can use an asynchronous generator. The syntax is simple: prepend function* with async. That makes the generator asynchronous. And then use for await (…) to iterate over it, like this: As the generator is asynchronous, we can use await inside it, rely on promises, perform network requests and so on. result = await generator.next(); // result = {value: …, done: true/false}

Async iterable range

Regular generators can be used as Symbol.iterator to make the iteration code shorter. Similar to that, async generators can be used as Symbol.asyncIterator to implement the asynchronous iteration. For instance, we can make the range object generate values asynchronously, once per second, by replacing synchronous Symbol.iterator with asynchronous Symbol.asyncIterator: Now values come with a delay of 1 second between them.

Real-life example: paginated data

So far we’ve seen basic examples, to gain understanding. Now let’s review a real-life use case. 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. 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: - We should make a request to fetch in the form https://api.github.com/repos//commits. - It responds with a JSON of 30 commits, and also provides a link to the next page in the Link header. - Then we can use that link for the next request, to get more commits, and so on. For our code, we’d like to have a simpler way to get commits. Let’s make a function fetchCommits(repo) that gets commits for us, making requests whenever needed. And let it care about all pagination stuff. For us it’ll be a simple async iteration for await..of. So the usage will be like this: Here’s such function, implemented as async generator: More explanations about how it works: 1. We use the browser fetch method to download the commits. - The initial URL is https://api.github.com/repos//commits, and the next page will be in the Link header of the response. - The fetch method allows us to supply authorization and other headers if needed – here GitHub requires User-Agent. 2. The commits are returned in JSON format. 3. We should get the next page URL from the Link header of the response. It has a special format, so we use a regular expression for that (we will learn this feature in Regular expressions). - The next page URL may look like https://api.github.com/repositories/93253246/commits?page=2. It’s generated by GitHub itself. 4. Then we yield the received commits one by one, and when they finish, the next while(url) iteration will trigger, making one more request. An example of use (shows commit authors in console): That’s just what we wanted. The internal mechanics of paginated requests is invisible from the outside. For us it’s just an async generator that returns commits.

Summary

Regular iterators and generators work fine with the data that doesn’t take time to generate. When we expect the data to come asynchronously, with delays, their async counterparts can be used, and for await..of instead of for..of. Syntax differences between async and regular iterators: Syntax differences between async and regular generators: In web-development we often meet streams of data, when it flows chunk-by-chunk. For instance, downloading or uploading a big file. We can use async generators to process such data. It’s also noteworthy that in some environments, like in browsers, there’s also another API called Streams, that provides special interfaces to work with such streams, to transform the data and to pass it from one stream to another (e.g. download from one place and immediately send elsewhere).

let range = {
  from: 1,
  to: 5
};
Example:

Follow the lesson from Microsoft Web-Dev-For-Beginners course

Tags: async