Last update:2024-08-14 19:50:29
The ReadableStreamDefaultReader
interface provides a way to read data from a ReadableStream
in a pull-based manner. It is based on the Web APIs standard ReadableStreamDefaultReader
interface.
Note:
You cannot directly construct a ReadableStreamDefaultReader
object. Use the ReadableStream.getReader
method to obtain one:
const { readable, writable } = new TransformStream();
const reader = readable.getReader();
closed
Promise<void>
(read-only)read()
reader.read(): Promise<{ value: any, done: boolean }>
Return Value: Returns a Promise containing the read data (chunk) and the reading status.
Description: Reads data from the stream.
Note: Calling the read()
method to initiate another read operation before the previous read operation completes is not allowed.
The reader.read
method returns a Promise
that resolves with an object in the format of { value: theChunk, done: boolean }
, where:
Promise
will be fulfilled with done: false
.Promise
will be fulfilled with value: undefined
and done: true
.Promise
will be rejected and contain the relevant error information.cancel()
reader.cancel(reason?: string): Promise<void>
Parameters:
reason
(optional): A human-readable string indicating the reason for cancellation.Description: Cancels the stream. The reason
is passed to the underlying source’s cancel algorithm. If this readable stream is one side of a TransformStream
, then its cancellation algorithm causes the transform’s writable side to become errored with the reason
.
Warning: Any data not yet read will be lost.
releaseLock()
reader.releaseLock(): void;
Description: Releases the lock on the readable stream.
Note: A lock cannot be released if the reader has pending read operations. If this occurs, a TypeError
is thrown, and the reader remains locked.