streams
streams copied to clipboard
Clarify expected behaviour when breaking async iteration of teed ReadableStream
What is the issue with the Streams Standard?
Is it intended by the spec that breaking async iteration of a teed stream halts the execution of the program until all branches are cancelled?
const rs = new ReadableStream({
start(controller) {
let i = 0;
const interval = setInterval(() => {
controller.enqueue(i++);
}, 100);
// Clean up when canceled
this._cancel = () => {
clearInterval(interval);
console.log('cancelled');
};
},
cancel(reason) {
console.log('cancel() called with', reason);
this._cancel?.();
return Promise.resolve();
},
});
const [r1, r2] = rs.tee();
let i = 0;
(async () => {
for await (const val of r1) {
console.log('r1', val);
if (i++ > 2) {
console.log('breaking r1');
break;
}
}
console.log('finished r1 loop');
})();
Right now in all environments I tested in (browsers and across NodeJS versions) the last log I get is "breaking r1" - finished r1 loop never gets logged