Rules Hub
Code Ruleset
Rule priority, scope & exceptions
Use this to align rules with the senior-level structure (P0/P1/P2, scope, exceptions/tradeoffs).
Make timeouts abortable via an AbortSignal
Wire timeouts to an AbortSignal so a single controller.abort() can cancel multiple pending timers at once.
Bad example
| 1 | const t1 = setTimeout(showFn, 300); |
| 2 | const t2 = setTimeout(hideFn, 300); |
| 3 | // must track and clear each id separately |
Explanation (EN)
Separately managed timers can outlive their owning operation or leak abort listeners.
Objašnjenje (HR)
Good example
| 1 | function setAbortableTimeout(fn: () => void, ms: number, signal: AbortSignal) { |
| 2 | if (signal.aborted) return; |
| 3 | const cancel = () => clearTimeout(id); |
| 4 | const id = setTimeout(() => { |
| 5 | signal.removeEventListener("abort", cancel); |
| 6 | fn(); |
| 7 | }, ms); |
| 8 | signal.addEventListener("abort", cancel, { once: true }); |
| 9 | } |
Explanation (EN)
Pre-aborted operations schedule nothing; completed and canceled timers release their listener.
Objašnjenje (HR)
Notes (EN)
Use a shared AbortSignal for timers that belong to the same cancellable operation; handle a pre-aborted signal and remove listeners when timers settle.
Exceptions / Tradeoffs (EN)
Use ordinary clearTimeout for an isolated timer; do not introduce a helper without shared lifecycle value.