Skip to content

createTextStream

createTextStream(response, options): Promise<string>

Defined in: stream-utils.ts:47

Create a controlled text stream from a fetch response with streaming body.

This function reads a streaming response body chunk by chunk, decodes the bytes as UTF-8 text, and provides callbacks for processing each chunk as it arrives. It accumulates the full text and returns it when the stream completes. Supports abort signals for cancellation and error handling.

Response

Fetch Response object with a streaming body

CreateTextStreamOptions = {}

Configuration including chunk/complete/error callbacks and abort signal

Promise<string>

Promise resolving to the complete accumulated text

If response body is null or if stream reading fails

const response = await fetch('/api/stream');
const fullText = await createTextStream(response, {
onChunk: (chunk) => console.log('Received:', chunk),
onComplete: (text) => console.log('Done:', text),
onError: (err) => console.error('Error:', err),
signal: abortController.signal
});