Rules Hub
Code Ruleset
Rule priority, scope & exceptions
Use this to align rules with the senior-level structure (P0/P1/P2, scope, exceptions/tradeoffs).
Merge cancellation signals with AbortSignal.any
Combine multiple cancellation sources (timeouts, user actions) into a single signal using AbortSignal.any to ensure robust resource cleanup.
Bad example
| 1 | async function fetchWithTimeout(url: string, parentSignal?: AbortSignal) { |
| 2 | const timeoutCtrl = new AbortController(); |
| 3 | const id = setTimeout(() => timeoutCtrl.abort(), 5000); |
| 4 |
|
| 5 | try { |
| 6 | // BAD: The fetch only respects the timeout. |
| 7 | // If the user navigates away (triggering parentSignal), the request continues running. |
| 8 | const res = await fetch(url, { signal: timeoutCtrl.signal }); |
| 9 | return res.json(); |
| 10 | } finally { |
| 11 | clearTimeout(id); |
| 12 | } |
| 13 | } |
Explanation (EN)
Using only the timeout signal discards cancellation from the owning lifecycle.
Objašnjenje (HR)
Korištenje samo timeout signala odbacuje otkazivanje vlasničkog životnog ciklusa.
Good example
| 1 | async function fetchWithTimeout(url: string, parentSignal?: AbortSignal) { |
| 2 | const timeout = new AbortController(); |
| 3 | const timer = setTimeout(() => timeout.abort(), 5000); |
| 4 | try { |
| 5 | const signals = parentSignal ? [parentSignal, timeout.signal] : [timeout.signal]; |
| 6 | const signal = AbortSignal.any(signals); |
| 7 | const response = await fetch(url, { signal }); |
| 8 | if (!response.ok) throw new Error("Request failed"); |
| 9 | return await response.json(); |
| 10 | } finally { |
| 11 | clearTimeout(timer); |
| 12 | } |
| 13 | } |
Explanation (EN)
The supported-runtime example combines both signals and keeps the deadline active until body parsing settles.
Objašnjenje (HR)
U podržanom okruženju primjer kombinira oba signala i zadržava rok do završetka parsiranja tijela.
Notes (EN)
Combine independent parent/user/deadline signals when the operation must stop for any of them, using an API supported by the actual runtime baseline.
Bilješke (HR)
Kombiniraj neovisne signale roditelja, korisnika i roka kada bilo koji treba prekinuti operaciju. Koristi API podržan u stvarnom izvršnom okruženju.
Exceptions / Tradeoffs (EN)
A manual fallback must handle already-aborted inputs, preserve the reason and remove listeners on settlement. Returning an unawaited body promise from try/finally can clear a manual deadline before body consumption finishes.