Rules Hub
Code Ruleset
Rule priority, scope & exceptions
Use this to align rules with the senior-level structure (P0/P1/P2, scope, exceptions/tradeoffs).
Assume universal support for AbortController
Use AbortController without a guard when the supported runtime baseline guarantees it; keep compatibility handling for actual unsupported environments.
Bad example
| 1 | useEffect(() => { |
| 2 | let controller: AbortController | undefined; |
| 3 |
|
| 4 | // Unnecessary feature detection for a standard API |
| 5 | if ('AbortController' in window) { |
| 6 | controller = new AbortController(); |
| 7 | } |
| 8 |
|
| 9 | const signal = controller?.signal; |
| 10 |
|
| 11 | fetch('/api/data', { signal }); |
| 12 | }, []); |
Explanation (EN)
A redundant feature branch complicates cancellation when the environment already guarantees the controller.
Objašnjenje (HR)
Suvišna grana podrške komplicira otkazivanje kada okruženje već jamči AbortController.
Good example
| 1 | useEffect(() => { |
| 2 | // Instantiate directly; no checks needed |
| 3 | const controller = new AbortController(); |
| 4 | const { signal } = controller; |
| 5 |
|
| 6 | fetch('/api/data', { signal }); |
| 7 |
|
| 8 | return () => controller.abort(); |
| 9 | }, []); |
Explanation (EN)
Instantiate AbortController directly. This simplifies the code, removes undefined checks, and relies on modern standard compliance.
Objašnjenje (HR)
Instanciraj AbortController izravno. To pojednostavljuje kod, uklanja provjere za undefined i oslanja se na moderne standarde.
Notes (EN)
Use AbortController directly when every supported execution environment guarantees it, and abort outstanding work during the relevant lifecycle cleanup.
Exceptions / Tradeoffs (EN)
Older embedded browsers, non-web runtimes or tests may require a polyfill/adapter. An async function name or type declaration is not evidence that the runtime provides this API.