Documentation Index
Fetch the curated documentation index at: https://grafana_com_website/llms.txt
Fetch the complete documentation index at: https://grafana_com_website/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: /docs/k6/latest/javascript-api/k6-experimental/streams/readablestream/cancel.md (append .md) or send Accept: text/markdown to /docs/k6/latest/javascript-api/k6-experimental/streams/readablestream/cancel/. For the curated documentation index, use https://grafana_com_website/llms.txt. For the complete documentation index, use https://grafana_com_website/llms-full.txt.
cancel(reason)
The cancel() method of the
ReadableStream interface returns a Promise that resolves when the stream is canceled.
Cancel is used when you’ve completely finished with the stream and don’t need any more data from it, even if chunks are enqueued waiting to be read. That data is lost after cancel is called, and the stream is not readable any more. To close the stream without getting rid of enqueued chunks, use
ReadableStreamDefaultController.close().
Arguments
| Name | Type | Description |
|---|---|---|
| reason | any | An optional human-readable value that represents the reason for canceling the stream. |
Return value
A promise that resolves with the value undefined when the stream is canceled.
Exceptions
| Exception | Description |
|---|---|
| TypeError | Thrown the stream is locked to a reader. |
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();
const reader = stream.getReader();
while (true) {
const { done, value } = await reader.read();
if (done) break;
if (value === 8) {
// Cancel the stream when the number is 8, any enqueued chunks are lost
await reader.cancel('cancelling the stream');
break;
}
}
}
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

