ReadableStreamDefaultReader
The ReadableStreamDefaultReader interface of Azion Runtime: its constructor, the closed property, and the methods that read, cancel, and release a stream.
The ReadableStreamDefaultReader interface is the default reader of the Streams API in Azion Runtime. It reads the data of a stream that comes from the network, such as the body of a response to a fetch() request. A ReadableStreamBYOBReader works only with a stream that has a byte source, but a ReadableStreamDefaultReader reads from any type of ReadableStream source. For more information, refer to ReadableStreamDefaultReader on MDN Web Docs.
Constructor
The ReadableStreamDefaultReader() constructor creates and returns a reader for the stream you pass to it:
The getReader() method of a ReadableStream also returns a ReadableStreamDefaultReader and locks the stream to it.
Properties
| Property | Type | Description |
|---|---|---|
closed | Promise | Fulfills when the stream closes. Rejects when the stream throws an error or when the lock of the reader is released. Use it to run code when the stream ends. |
Methods
| Method | Description |
|---|---|
reader.cancel(reason) | Returns a promise that resolves when the stream is canceled. A call signals that the consumer has lost interest in the stream. The underlying source receives reason and can use it or ignore it. |
reader.read() | Returns a promise that gives access to the next chunk in the internal queue of the stream, as an object with value and done. After the last chunk, done is true and value is undefined. |
reader.releaseLock() | Releases the lock of the reader on the stream. After the call, the locked property of the stream is false. |
Example
This handler fetches a page, reads its body one chunk at a time with a reader, and adds each chunk to a stream it constructs. It returns that stream as the body of a Response with an HTML Content-Type header:
The response that the function returns has these values: its status, its content-type header, the length of its body in characters, and the first 15 characters of the body: