Asynchronous generators
Explanation of how we can create asynchronous generators in JavaScript
Similar to the difference between synchronous iterators and async iterators, the main difference between a regular generator and an async generator is that an async generator yields a promise.
Async generators are a combination of async
functions and generators. As a result, we can use both the await
and the yield
keywords inside an async generator. Calling an async generator returns an AsyncGenerator object that implements both async iterator as well as async iterable protocols. The next method of the AsyncGenerator
object returns a promise.
Let us rewrite the async iterator example in the previous lesson to use an async generator:
const BASE_URL = "https://jsonplaceholder.typicode.com/users";
Here's a Replit of the code above:
This lesson preview is part of the Advanced JavaScript Unleashed course and can be unlocked immediately with a \newline Pro subscription or a single-time purchase. Already have access to this course? Log in here.
Get unlimited access to Advanced JavaScript Unleashed, plus 70+ \newline books, guides and courses with the \newline Pro subscription.
