Async iteration and generators - javascript.info
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:
1 | let range = { |
…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 thenext
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:<loop value>}
, wheredone:true
means the end of the loop.
Here’s an implementation for the iterable range
:
1 | let range = { |
If anything is unclear, please visit the chapter Iterables, 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:
-
Use
Symbol.asyncIterator
instead ofSymbol.iterator
. -
The
next()
method should return a promise (to be fulfilled with the next value).- The
async
keyword handles it, we can simply makeasync next()
.
- The
-
To iterate over such an object, we should use a
for await (let item of iterable)
loop.- Note the
await
word.
- Note the
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:
1 | let range = { |
As we can see, the structure is similar to regular iterators:
- To make an object asynchronously iterable, it must have a method
Symbol.asyncIterator
(1)
. - This method must return the object with
next()
method returning a promise(2)
. - The
next()
method doesn’t have to beasync
, it may be a regular method returning a promise, butasync
allows us to useawait
, so that’s convenient. Here we just delay for a second(3)
. - To iterate, we use
for await(let value of range)
(4)
, namely add “await” after “for”. It callsrange[Symbol.asyncIterator]()
once, and then itsnext()
for values.
Here’s a small table with the differences:
Iterators | Async iterators | |
---|---|---|
Object method to provide iterator | Symbol.iterator |
Symbol.asyncIterator |
next() return value is |
any value | Promise |
to loop, use | for..of |
for await..of |
The spread syntax ...
doesn’t work asynchronously
Features that require regular, synchronous iterators, don’t work with asynchronous ones.
For instance, a spread syntax won’t work:
1 | alert([...range]); // Error, no Symbol.iterator |
That’s natural, as it expects to find Symbol.iterator
, not Symbol.asyncIterator
.
It’s also the case for for..of
: the syntax without await
needs 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.
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
:
1 | function* generateSequence(start, end) { |
As we already know, to make an object iterable, we should add Symbol.iterator
to it.
1 | let range = { |
A common practice for Symbol.iterator
is to return a generator, it makes the code shorter, as you can see:
1 | let range = { |
Please see the chapter Generators 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:
1 | async function* generateSequence(start, end) { |
As the generator is asynchronous, we can use await
inside it, rely on promises, perform network requests and so on.
Under-the-hood difference
Technically, if you’re an advanced reader who remembers the details about generators, there’s an internal difference.
For async generators, the generator.next()
method is asynchronous, it returns promises.
In a regular generator we’d use result = generator.next()
to get values. In an async generator, we should add await
, like this:
1 | result = await generator.next(); // result = {value: ..., done: true/false} |
That’s why async generators work with for await...of
.
# 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
:
1 | let range = { |
Now values come with a delay of 1 second between them.
Please note:
Technically, we can add both Symbol.iterator
and Symbol.asyncIterator
to the object, so it’s both synchronously ( for..of
) and asynchronously ( for await..of
) iterable.
In practice though, that would be a weird thing to do.
# 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 formhttps://api.github.com/repos/<repo>/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:
1 | for await (let commit of fetchCommits("username/repository")) { |
Here’s such function, implemented as async generator:
1 | async function* fetchCommits(repo) { |
More explanations about how it works:
- We use the browser fetch method to download the commits.
- The initial URL is
https://api.github.com/repos/<repo>/commits
, and the next page will be in theLink
header of the response. - The
fetch
method allows us to supply authorization and other headers if needed – here GitHub requiresUser-Agent
.
- The initial URL is
- The commits are returned in JSON format.
- 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.
- The next page URL may look like
- 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):
1 | (async () => { |
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:
Iterable | Async Iterable | |
---|---|---|
Method to provide iterator | Symbol.iterator |
Symbol.asyncIterator |
next() return value is |
{value:…, done: true/false} |
Promise that resolves to {value:…, done: true/false} |
Syntax differences between async and regular generators:
Generators | Async generators | |
---|---|---|
Declaration | function* |
async function* |
next() return value is |
{value:…, done: true/false} |
Promise that resolves to {value:…, done: true/false} |
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).