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)

👋 Kindness is contagious

Engage with a sea of insights in this enlightening article, highly esteemed within the encouraging DEV Community. Programmers of every skill level are invited to participate and enrich our shared knowledge.

A simple "thank you" can uplift someone's spirits. Express your appreciation in the comments section!

On DEV, sharing knowledge smooths our journey and strengthens our community bonds. Found this useful? A brief thank you to the author can mean a lot.

Okay