Re: [whatwg/streams] Interesting edge case... queueMicrotask and desiredSize in start algorithm (Issue #1362)

MattiasBuelens left a comment (whatwg/streams#1362)

Correct, the call to `reader.read()` happens first.
```javascript
const rs = new ReadableStream({
  start(c) { queueMicrotask(/* ... */) },
  pull(c) { /* ... */ }
}, { highWaterMark: 1 });

const reader = rs.getReader();
const readPromise = reader.read(); // <<< happens synchronously
console.log(await readPromise);
```
This adds a read request to `reader.[[readRequests]]`, so the first call to `controller.enqueue()` will immediately fulfill that request and bypass `stream.[[queue]]` (see [step 3 of ReadableStreamDefaultControllerEnqueue](https://streams.spec.whatwg.org/#readable-stream-default-controller-enqueue)). Hence, the desired size remains unchanged.

If you run the same code without the `queueMicrotask`, then the call to `controller.enqueue()` happens before the first `reader.read()`. Hence, the chunk ends up in the queue, and the desired size decreases. When `reader.read()` is called, it is fulfilled immediately from the queue.

I hope that explains it?

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

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

Received on Thursday, 15 January 2026 11:10:34 UTC