Overview
Basic Concepts
Quick Start
Manage Functions In Edge Cloud Apps
Function Deployment
Trigger Your Functions
Edge KV
Developer Tools
Examples
Runtime APIs
Cookies
Encoding
Fetch
Headers
HTMLRewriter
Request
Response
Streams
ReadableStream
ReadableStreamBYOBReader
ReadableStreamDefaultReader
TransformStream
WritableStream
WritableStreamDefaultWriter
Web Crypto
Web standards
WebSockets
EdgeKV
CloudIDE
Limits

ReadableStreamDefaultReader

最終更新日: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();

Properties

closed

  • Type: Promise<void> (read-only)
  • Description: A promise that resolves when the stream is closed and rejects if there is an error in the stream or the reader’s lock is released.

Methods

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:

  • If a chunk is available, the Promise will be fulfilled with done: false.
  • If the stream is closed, the Promise will be fulfilled with value: undefined and done: true.
  • If the stream encounters an error, the 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.

References