liveBook · Manning

Async iterable

As explained in Build AI-Enhanced Web Apps

An async iterable is an object that asynchronously produces values for consumption through asynchronous iteration.

An async iterable asynchronously produces values for consumption with for await...of. In streaming applications, each yielded value can be processed or displayed as soon as it arrives. section 3.2.2

A practical async-iterable flow has three moving parts:

  • An async generator or streaming API produces values over time. section 3.2.2
  • Each yield makes the next value available without waiting for the complete result. section 3.2.2
  • A for await...of loop consumes values until the iterable finishes; infinite generators require an explicit termination condition. section 3.2.2

Streaming generated text

Calling streamText returns textStream, an async iterable. Iterating with for await...of lets an application write each textPart to the output immediately instead of waiting for the full response. section 3.2.2

Listing 3.3 streamText example
import { createGoogleGenerativeAI } from '@ai-sdk/google';
import { streamText } from 'ai';    #1

const model = createGoogleGenerativeAI({
  apiKey: process.env.GEMINI_API_KEY || '',
}); 
const { textStream } = await streamText({.   #2
  model: model('gemini-2.0-flash'),
  prompt: 'Generate 400 words of random content for me please',
});

for await (const textPart of textStream) {.   #3
  process.stdout.write(textPart);
}

Inspect the runnable streamText pattern, including model setup, environment configuration, asynchronous iteration, and incremental output. section 3.2.2

How Build AI-Enhanced Web Apps covers it

How deep it goes
Explains async generators, delayed yields, loop completion, and termination warnings, while deferring deeper iterator internals.
Examples
Uses a delayed one-to-three generator and a streamText Gemini example that writes generated chunks incrementally.
What you can do after
Apply it via JavaScript for await...of to consume delayed values or streamed AI text incrementally.
How it connects
Connects asynchronous iteration with async generators, for await...of, streaming text, and React chat interfaces.

Also explained in

Compare how 2 books explain async iterable →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Build AI-Enhanced Web Apps. The section links point to the parts of the book it draws on.