Documentation for automated readers
A curated documentation index is available at: https://grafana.com/llms.txt
A complete documentation index is available at: https://grafana.com/llms-full.txt
These indexes can help with page discovery before fetching individual documents.
This page is also available in Markdown, which may be easier for automated readers and AI tools to parse than HTML. The Markdown version is available at https://grafana.com/docs/k6/latest/javascript-api/k6-experimental/streams/readablestreamdefaultreader/releaselock.md, or by sending Accept: text/markdown to https://grafana.com/docs/k6/latest/javascript-api/k6-experimental/streams/readablestreamdefaultreader/releaselock/. For broader documentation discovery, the curated index is available at https://grafana.com/llms.txt and the complete index is available at https://grafana.com/llms-full.txt.
releaseLock()
The releaseLock() method of the
ReadableStreamDefaultReader interface releases the reader’s lock on the stream.
If the associated stream is errored as the lock is released, the reader will be errored as well. This method is useful when you’re done with the stream and want to release the lock on it.
If the reader’s lock is released as pending read operations are still in progress, the reader’s
ReadableStreamDefaultReader.read() calls are immediately rejected with a TypeError.
Exceptions
| Exception | Description |
|---|---|
| TypeError | Thrown when the source object is not a ReadableStreamDefaultReader |
Example
import { ReadableStream } from 'k6/experimental/streams';
import { setTimeout } from 'k6/timers';
export default async function () {
// Define a number stream that emits numbers from 1 to 10 every second
const stream = numbersStream();
// We use the getReader method to create a reader and lock the stream to it
const reader = stream.getReader();
while (true) {
const { done, value } = await reader.read();
if (done) break;
}
// Release the lock on the stream, so we're free to obtain another reader
// and read from the stream again
reader.releaseLock();
}
function numbersStream() {
let currentNumber = 0;
return new ReadableStream({
start(controller) {
const fn = () => {
if (currentNumber < 10) {
controller.enqueue(++currentNumber);
setTimeout(fn, 1000);
return;
}
controller.close();
};
setTimeout(fn, 1000);
},
});
}Was this page helpful?
Related resources from Grafana Labs

