Home  >  Article  >  Web Front-end  >  Why `response.json()` must be awaited

Why `response.json()` must be awaited

Barbara Streisand
Barbara StreisandOriginal
2024-11-06 08:31:02614browse

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)

The above is the detailed content of Why `response.json()` must be awaited. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn