Skip to content

Clarify expected behaviour when breaking async iteration of teed ReadableStream #1343

Description

@lukasIO

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

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions