This is documentation for the next version of K6. For the latest stable release, go to the latest version.
cancel(reason)
The cancel()
method of the ReadableStreamDefaultReader 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. |
Returns
A promise that resolves with the provided reason
when the stream is canceled.
Exceptions
Exception | Description |
---|---|
TypeError | Thrown when the source object is not a ReadableStreamDefaultReader or the stream has no owner. |
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;
if (value === 8) {
// Cancel the stream when the number is 8
await reader.cancel('cancelling the stream');
break;
}
console.log(`received number ${value} from stream`);
}
}
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);
},
});
}