DEV Community

Cover image for Why `response.json()` must be awaited
Bosco Domingo
Bosco Domingo

Posted on

1

Why `response.json()` must be awaited

Credit to the source, I'm just putting it in writing for easier access!

It turns out json() must be awaited because fetch() will resolve or reject when it receives the response headers, regardless of what the body is! The body is likely to be much larger in size, and may even take a long time to arrive and/or might do so in chunks, thus it has to be awaited even after fetch() has finished.

I had no idea why this behaviour existed until now. I love learning deeper stuff like this, and hope you do too!

Again, watch the video for a better and more visual explanation! (it's 6 minutes long, worth every second)

Top comments (0)

11 Tips That Make You a Better Typescript Programmer

typescript

1 Think in {Set}

Type is an everyday concept to programmers, but it’s surprisingly difficult to define it succinctly. I find it helpful to use Set as a conceptual model instead.

#2 Understand declared type and narrowed type

One extremely powerful typescript feature is automatic type narrowing based on control flow. This means a variable has two types associated with it at any specific point of code location: a declaration type and a narrowed type.

#3 Use discriminated union instead of optional fields

...

Read the whole post now!

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay