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.
Parameters
Section titled “Parameters”response
Section titled “response”Response
Fetch Response object with a streaming body
options
Section titled “options”Configuration including chunk/complete/error callbacks and abort signal
Returns
Section titled “Returns”Promise<string>
Promise resolving to the complete accumulated text
Throws
Section titled “Throws”If response body is null or if stream reading fails
Example
Section titled “Example”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});