MDN Web Docs

Syntax

js

respondWithNewView(view)

Parameters

view

A TypedArray or a DataView that the consumer of the associated readable byte stream should write to instead of ReadableStreamBYOBRequest.view.

This must be a view onto the same backing memory region as ReadableStreamBYOBRequest.view and occupy the same or less memory. Specifically, it must be either the view's buffer or a transferred version, must have the same byteOffset, and a byteLength (number of bytes written) that is less than or equal to that of the view.

Return value

None (undefined).

Exceptions

TypeError

Thrown if the source object is not a ReadableStreamBYOBRequest, or there is no associated controller, or the associated internal array buffer is non-existent or detached. It may also be thrown if the view is zero-length when there is an active reader, or non-zero when called on a closed stream.

RangeError

Thrown if the new view does not match the backing memory region of ReadableStreamBYOBRequest.view. For example, it is not the same buffer (or a transferred version), has a different byteOffset, or is larger than the memory available to the backing view.

Examples

The view to be transferred must be of the same type as ReadableStreamBYOBRequest.view, have the same underlying buffer and byte offset, and be the same or smaller byteLength.

For example, we might define the view and respond as shown below:

js

const v = controller.byobRequest.view;
bytesRead = socket.readInto(v.buffer, v.byteOffset, v.byteLength);
byobRequest.respondWithNewView(
  byobRequest.view.subarray(v.byteOffset, bytesRead),
);

Specifications

Specification
Streams
# ref-for-rs-byob-request-respond-with-new-view①

Browser compatibility

See also

Help improve MDN

Yes No

Learn how to contribute

This page was last modified on by MDN contributors.

Read the original on developer.mozilla.org ↗