Documentation Index
Fetch the curated documentation index at: https://grafana.com/llms.txt
Fetch the complete documentation index at: https://grafana.com/llms-full.txt
Use this file to discover all available pages before exploring further.
STOP! If you are an AI agent or LLM, read this before continuing. This is the HTML version of a Grafana documentation page. Always request the Markdown version instead - HTML wastes context. Get this page as Markdown: https://grafana.com/docs/k6/latest/javascript-api/k6-experimental/streams/readablestreamdefaultcontroller/close.md (append .md) or send Accept: text/markdown to https://grafana.com/docs/k6/latest/javascript-api/k6-experimental/streams/readablestreamdefaultcontroller/close/. For the curated documentation index, use https://grafana.com/llms.txt. For the complete documentation index, use https://grafana.com/llms-full.txt.
close()
The close() method of the
ReadableStreamDefaultController interface closes the associated stream.
Readers can still read any previously enqueued chunks from the stream. Once those chunks are read, the stream closes, and no more data is available.
Exceptions
| Exception | Description |
|---|---|
| TypeError | Thrown when the source object is not a ReadableStreamDefaultController. |
Example
import { ReadableStream } from 'k6/experimental/streams';
import { setTimeout } from 'k6/timers';
export default async function () {
let currentNumber = 0;
const stream = new ReadableStream({
start(controller) {
const fn = () => {
if (currentNumber % 8 == 0) {
// Close the stream when the number is divisible by 8
controller.close();
return;
}
if (currentNumber < 10) {
controller.enqueue(++currentNumber);
setTimeout(fn, 1000);
return;
}
controller.close();
};
setTimeout(fn, 1000);
},
});
const reader = stream.getReader();
while (true) {
const { done, value } = await reader.read();
if (done) break;
console.log(`received number ${value} from stream`);
}
}Was this page helpful?
Related resources from Grafana Labs

