What are async generators, and how do they work?

Async generators allow the await keyword inside generators, enabling asynchronous iteration. They yield promises and can be used in for await...of loops, making it easier to handle asynchronous sequences.

async function* asyncGen() {
  yield await Promise.resolve(1);
  yield await Promise.resolve(2);
}
(async () => {
  for await (const val of asyncGen()) {
    console.log(val); // 1, then 2
  }
})();