CKTIS / JavaScript
ReadableStream.getReader(): readers and stream locking
getReader() returns a reader and locks the stream. Reading is asynchronous; releaseLock() releases the lock without canceling the stream.
Version: ECMAScript and Web APIs; exact feature edition and browser support reviewed per entry; Node 24 in repository does not prove browser API support
Syntax
stream.getReader()
stream.getReader({ mode: "byob" })stream.getReader() synchronously returns a ReadableStreamDefaultReader. The lock prevents acquiring another reader; attempting this throws TypeError.
reader.read() returns a promise for { value, done }. done: true means completion; rejection signals an error. Release the lock with reader.releaseLock() in finally. Releasing does not cancel the stream.
stream.getReader({ mode: "byob" }) returns a ReadableStreamBYOBReader: read(view) accepts a buffer view. BYOB mode requires a readable byte stream, regardless of how it was created; calling getReader() in this mode on a non-byte stream throws TypeError. Setting type: "bytes" is one way to create such a stream. A non-null Fetch response body can be a readable byte stream without the caller having to create it. Merely enqueueing Uint8Array chunks is insufficient.
This is a WHATWG Web API, not a feature of a particular ECMAScript edition. Check default-reader and BYOB availability separately in the target environment.
Examples
async function main() {
const stream = new ReadableStream({
start(controller) {
controller.enqueue("A");
controller.enqueue("B");
controller.close();
}
});
const reader = stream.getReader();
let text = "";
try {
while (true) {
const { value, done } = await reader.read();
if (done) break;
text += value;
}
} finally {
reader.releaseLock();
}
console.log(text);
console.log(stream.locked);
}
main().catch(console.error);Result
AB false
The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.