getReader()
Open source
getReader()
Creates a reader and locks the stream to it. While the stream is locked, no other reader can be acquired until this one is released.
Arguments
The getReader
method takes a single optional options
argument, which is an object with the following properties:
Name | Type | Default value | Description |
---|---|---|---|
mode | string | undefined | A property that specifies the type of reader to create. It currently only supports the undefined value. |
Return value
A ReadableStreamDefaultReader object instance.
Example
JavaScript
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;
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);
},
});
}
Was this page helpful?
Related resources from Grafana Labs
Additional helpful documentation, links, and articles:
08 May

Without a trace (query): Troubleshooting simplified with Tempo and Traces Drilldown in Grafana
See how Traces Drilldown in Grafana, powered by Grafana Tempo, can simplify complex troubleshooting, helping you quickly detect latency issues, identify error trends, and spot anomalies – all without having to learn yet another query language.
60 min

Getting started with tracing and Grafana Tempo
In this webinar, we'll show you how to get started setting up Grafana Tempo, our open source, easy-to-use and high-volume distributed tracing backend.
60 min

Scaling your distributed tracing with Grafana Tempo
In this demo, we’ll show how Grafana Tempo allows you to scale tracing as far as possible with less operational cost and complexity than ever before.