Body.json
returns a promise.
Is this method asynchronous to avoid blocking on reading large inbound data streams?
Does it do something like setTimeout(sampleStream, 0)
repeatedly until the end of the stream is found?
Body.json
returns a promise.
Is this method asynchronous to avoid blocking on reading large inbound data streams?
Does it do something like setTimeout(sampleStream, 0)
repeatedly until the end of the stream is found?
Is
.json()
asynchronous?
Yes. That's why it returns a promise.
Is this method asynchronous to avoid blocking on reading large inbound data streams?
Yes. You receive the Response
right after the headers arrived, and receiving the body might take some time.
Does it do something like
setTimeout(sampleStream, 0)
repeatedly until the end of the stream is found?
Not exactly. It doesn't use setTimeout
, it reads from the stream by repeatably getting promises for the next chunk - just check about reading all bytes from a ReadableStream yourself.
And most importantly, this all happens on a background task without any concerns for JavaScript. As the note on that section tells: Because the reader grants exclusive access, the actual mechanism of how to read cannot be observed. Implementations could use more direct mechanism if convenient.