Important: This documentation is about an older version. It's relevant only to the release noted, many of the features and functions have been updated or replaced. Please view the current version.
import{ ReadableStream }from'k6/experimental/streams';import{ setTimeout }from'k6/timers';exportdefaultasyncfunction(){// Define a number stream that emits numbers from 1 to 10 every secondconst stream =numbersStream();// We use the getReader method to create a reader and lock the stream to itconst reader = stream.getReader();while(true){const{ done, value }=await reader.read();if(done)break;
console.log(`received number ${value} from stream`);}}functionnumbersStream(){let currentNumber =0;returnnewReadableStream({start(controller){constfn=()=>{if(currentNumber <10){
controller.enqueue(++currentNumber);setTimeout(fn,1000);return;}
controller.close();};setTimeout(fn,1000);},});}