To avoid the XY problem, here's my final goal: I want to fetch something, use the response body, and return the response (from which the user should be able to get the body) without returning the body separately. On the paper, this should work:
const res = await fetch("something")
const clone = res.clone()
const body = await clone.json()
// Use body
return res
If I get lucky, the process ends during await clone.json()
. If I don't, it freezes. Forever.
As a reminder, res.json
can't be called twice.
Does it think I'm not good enough to get an error message? Any help on that? Thank you.
Progess
I located the error: in node_modules/node-fetch/lib/index.js:416
, the listener on the end of the action is never triggered. I still don't know why.
When replacing the URL with "https://jsonplaceholder.typicode.com/todos/1"
, everything works. This may be related to the server. I don't see any link between the origin of the request and whether the request could be cloned...
Placing res.text()
before clone.json()
magically fixes it, but it makes everything lose its purpose.
By messing with the module's code, I found that the response actually never ends. The JSON gets cut, and the chunk from the last data
event call isn't the end of the JSON, so the end
event never gets called. Extremely confusing.
I spent too much time on this issue, I will simply avoid it by returning the body separately.