4 ms·
I think it’s because of the different actions you might want to take after the fetch. 1. Parse the data as json 2. Return it without parsing 3. Modify some o
by thegagne 5y ago
I think it’s because of the different actions you might want to take after the fetch.
1. Parse the data as json
2. Return it without parsing
3. Modify some other part of the response but never parse the body
4. Turn around and stream that body into another fetch without parsing it.
Etc. parsing it and loading a large response into memory may not always be desirable so it must be explicitly done with await res.body.json().
- mcintyre1994 5y agoI agree that it might never be parsed/you might not want to, and that it shouldn’t be done implicitly. But I’m not sure why that requires await instead of just a plain function call to parse it explicitly.
- pininja 5y agoParsing JSON is synchronous, but fetch itself doesn’t read the body data. > [1] to parse the body as JSON, first the body data has to be read from the incoming stream. And, since reading from the TCP stream is asynchronous, the .json() operation ends up asynchronous. [1] https://stackoverflow.com/a/59555579 https://stackoverflow.com/a/59555579
- mcintyre1994 5y agoIs that basically the same point at a lower level though? I don't think reading the stream involves a round trip?