[whatwg/streams] When should closed promise be resolved? (Issue #1244)

I've got a question about stream reader `closed` promise, specifically whether it should resolve before all enqueued data has been read.

In the program below, I'm seeing the message `data not read yet` being logged. I was assuming `reader.closed` wouldn't get resolved until the program had read the data.

```js
const rs = new ReadableStream({
  start(controller) {
    controller.enqueue(new Uint8Array(3));
    controller.close();
  }
});

let data = null;
const reader = rs.getReader();

reader.closed.then(() => {
  if (!data) {
    console.error('data not read yet');
  }
});

data = await reader.read();
console.log(data);

data = await reader.read();
console.log(data);
```

I'm seeing this in Chrome, Firefox and Node.

So I read the spec and in https://streams.spec.whatwg.org/#rs-default-controller-private-pull ReadableStreamClose is performed before the read request's chunk steps which explains what I'm seeing. (Indeed I looked at Node's implementation and it follows the spec).

Is this how `closed` is supposed to behave? It has implications for the calling application. For example, Node uses it to push EOF in its stream adapter and this causes https://github.com/nodejs/node/issues/42694

-- 
Reply to this email directly or view it on GitHub:
https://github.com/whatwg/streams/issues/1244
You are receiving this because you are subscribed to this thread.

Message ID: <whatwg/streams/issues/1244@github.com>

Received on Sunday, 16 October 2022 05:52:27 UTC