Rules Hub
Code Ruleset
Rule priority, scope & exceptions
Use this to align rules with the senior-level structure (P0/P1/P2, scope, exceptions/tradeoffs).
Await response.json() before returning the parsed body
Await response.json() before using parsed data; directly returning its promise can be correct.
Bad example
| 1 | return { success: res.status === 200, data: res.json() }; |
Explanation (EN)
The data property contains a promise rather than the promised parsed payload.
Objašnjenje (HR)
Good example
| 1 | if (!res.ok) throw new Error(`Request failed: ${res.status}`); |
| 2 | const data = await res.json(); |
| 3 | return { success: true, data }; |
Explanation (EN)
For an endpoint that promises a JSON success body, parsing completes before data is placed in the result; validate its schema at the boundary.
Objašnjenje (HR)
Notes (EN)
Await body parsing before placing parsed data into an object or reading its properties; validate the status, body availability and expected schema.
Exceptions / Tradeoffs (EN)
Directly returning response.json() from a promise-returning function is valid and adopts its result. Empty 204/HEAD bodies cannot be blindly parsed as JSON.