Ranter
Join devRant
Do all the things like
++ or -- rants, post your own rants, comment on others' rants and build your customized dev avatar
Sign Up
Pipeless API
From the creators of devRant, Pipeless lets you power real-time personalized recommendations and activity feeds using a simple API
Learn More
Comments
-
retoor120111yJSON.parse(resp.text()) or smth. Should never give a different result than the axios fucker does.
Nice first rant. Kudos
Edit: just const txt = await data? -
const response = await fetch('thing')
const jsonBody = await response.json()
If my code above is not wrong, this will get your JSON thing as an actual object. The thing is, the response variable above, awaited by the fetch() promise, contains a "body" object which really is a stream (if I recall correctly), so that's why you gotta read it asynchronously. The json() method does the work of reading the whole JSON body stream and converting it into a JS object for you. (It'll fail parsing if the return isn't a JSON, though)
Also, since it's a stream, I THINK if you try to read the same JSON body out of response again, it'll no longer be there, because it was pulled out from the stream in your previous call.
It's something like that, hopefully I'm right AND explained in a clear way. -
@nururururu >it'll no longer be there
Imagine you had some shit stuck in a pipe, waiting to be removed in one end of the pipe. You open that end, and collect the shit with your hand. If you then try to remove shit again in the same pipeline of promises, no shit will be there. That's the nature of streams, one of which your network request is at the application level.
Hopefully I'm not too wrong here.
Related Rants
first of all fuck this stupid website and deleting your rant if you aren't signed up
second of all fuck fetch
curl gives me a readable json object
axios gives me a readable json object
fetch gives me what should be a readable json object, but looks more like a set in python instead
[{"id":"1"},{"id":"2"}] curl and axios reply with this
meanwhile fetch
{
[
{"id":"1"},
{"id":"2"}
],
{various symbol objects}
}
how am I supposed to get my data out of a fetch? I see people call response[0] or using some strange amalgamation of
fetch().then().then((data) => {}) but data in this results in an unresolved promise for some inexplicable reason. (nextjs)
also fuck nextjs I want to go back to hardcoding everything in html
also fuck modern web development and businesses in general, they ruined the internet.
devrant
axios
nextjs
curl
webdev
js
fetch