3 ms·
Why do you need to put await before res.json?
by Kiro 11y ago
Why do you need to put await before res.json?
- tomelders 11y agoSomeone correct me if I'm wrong here, this is just my hunch - but according to the fetch spec, res.json returns a promise, which are async. I'm assuming so you have the chance to handle errors as a result of malformed JSON.
- thomasahle 11y agoPromises are used for catching errors?
- onestone 11y agoYes.
- jwmerrill 11y agoAt the time that the request resolves, it has received and processed the response headers, but it may still be receiving the response body (which is represented as a stream). All the body representations (like .text() or .json()) need to wait until the body has arrived before they can resolve. https://fetch.spec.whatwg.org/#concept-body-consume-body https://fetch.spec.whatwg.org/#concept-body-consume-body