Response.json() never resolves
See original GitHub issueI have an interesting issue with request that essentially never finishes. It is tricky to reproduce, but everything boils down to proper timing such that there are response headers, but not response body yet.
In such circumstances I’ve uncovered 2 issues:
- Timeouts are not propagated to cloned responses, so those never time out (fixed by https://github.com/bitinn/node-fetch/pull/664)
- Sometimes even though complete body was received (
accumBytes
is the same asContent-Length
),body.on('end', ...)
is never fired
I’m not sure how to fix the second one, one reason may be that response.size
is 0
for some reason, looks like it is not read from headers or something. Hopefully maintainers can figure that out.
But at very least with mentioned PR merged it will handle timeouts on cloned response properly.
Reduced example of the code I’m dealing with is like this:
import fetch from "node-fetch";
(async () => {
const response = await fetch(
'http://domain.tld/path',
{
timeout: 2000,
},
);
console.log(response.status, response.headers);
console.log(await response.clone().json());
console.log('This may not be printed ever, without any errors');
})();
Issue Analytics
- State:
- Created 4 years ago
- Reactions:12
- Comments:7 (6 by maintainers)
Top Results From Across the Web
Fetch API not giving response data in Node.js
I have been trying to fix this but it gives me this: ReferenceError: json is not defined.
Read more >await - JavaScript - MDN Web Docs
The expression is resolved in the same way as Promise.resolve() : it's always converted to a native Promise and then awaited.
Read more >json() promise never gets resolved in events.js
Hi everyone ! In order to do some debugging, I gathered all my functions in one single piece of code. This is the...
Read more >Promise - TypeScript Deep Dive
In fact, JSON.parse throws an error if it is passed bad JSON and the callback never gets called and the application crashes. This...
Read more >Use Promise.all to Stop Async/Await from Blocking ...
Once the response comes back, it resolves a Promise with the data that came back. ... (response) => response.json() ); // .then still...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
The correct solution is to resolve cloned response in parallel. See our test cases for an example (search for Promise.all).
The lazy solution is to set a higher water mark during clone, this has been introduced in v3 also.
(From my phone)
Have to reopen, turns out it is still an issue and is related to chunked encoding apparently. So this works fine:
This will timeout if response headers contain
Transfer-Encoding: chunked
(in my case response from Discord that contains a lot of guild channels, likeGET https://discordapp.com/api/v6/guilds/X/channels
):Still investigating why exactly.