Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

AI Tools · Function

simulateReadableStream

Creates a ReadableStream that emits supplied values in order with optional initial and inter-chunk delays. It is mainly useful for testing streaming behavior or reproducing time-delayed responses.

How it works

The utility builds a generic ReadableStream from an ordered chunks array. It can pause before the first emission and between later emissions, or skip either delay with null, then closes after the final chunk.

Syntax

simulateReadableStream<T>({ chunks, initialDelayInMs?, chunkDelayInMs? }): ReadableStream<T>

Parameters

NameTypeDescription
chunksT[]Array of values to emit sequentially.
initialDelayInMsnumber | nullOptional milliseconds to wait before the first value; defaults to 0, or null to skip the initial delay.
chunkDelayInMsnumber | nullOptional milliseconds to wait between values; defaults to 0, or null to skip inter-chunk delays.

Returns

ReadableStream<T> that emits the provided chunks in sequence and closes after the last one.

Key points

  • It is a generic helper for producing ordered stream values while controlling simulated latency.
  • The required chunks option supplies the complete sequence the stream will publish.
  • initialDelayInMs affects only the pause before the first chunk.
  • chunkDelayInMs applies between consecutive chunks, not before the first one.
  • Consumers receive a normal completion after the final array element.

Common mistakes

  • Use null rather than relying on an omitted option when the first delay must be explicitly disabled.
  • A null chunk delay makes subsequent values arrive without an intentional pause.

Example: Simulate delayed text chunks

import { simulateReadableStream } from 'ai';

const stream = simulateReadableStream({
  chunks: ['Hello', ' ', 'world!'],
  initialDelayInMs: 100,
  chunkDelayInMs: 50,
});

for await (const chunk of stream) {
  process.stdout.write(chunk);
}

Creates a stream that emits three strings after an initial pause and then at fixed intervals.

When to use it

Streaming tests

Provide predictable delayed output while testing code that consumes streams.

Cached response playback

Recreate progressive model-like output from saved chunks.

Latency simulation

Exercise UI or transport behavior under controlled latency.

Interview questions

QWhat does simulateReadableStream do?

It accepts an array of values and returns a ReadableStream that emits them in order. The stream closes automatically once every value has been emitted.

Follow-up: How would you model a stream of structured events?

QHow do the delay options differ?

initialDelayInMs controls the wait before the first value, while chunkDelayInMs controls the wait between later values. Both default to zero when omitted.

Follow-up: How can you create a stream with no artificial timing?

QHow do you disable delays?

Set either delay option to null to omit that delay entirely. This is useful when tests need deterministic immediate delivery rather than simulated latency.

Follow-up: How could you test a consumer's handling of progressive output?

  • ReadableStream
  • streamText
  • MockLanguageModelV4

Source: simulate-readable-stream. Checked 2026-10-15.