Rules Hub
Coding Rules Library
← Back to all rules
Rule priority, scope & exceptions
Use this to align rules with the senior-level structure (P0/P1/P2, scope, exceptions/tradeoffs).
frontend ruleP1universalStack: React
reactpromisesmemory-leakasynclifecycle
Tie a stored Promise resolver to the component lifecycle
Storing a Promise resolver in a ref to await a dialog answer leaks the awaiting caller if the component unmounts or the handler re-enters; reject/clean up the pending resolver on unmount or re-entry.
PR: hegnar-forum-web · org-mining-3rd-2026-06Created: Jun 18, 2026
Bad example
Old codets
| 1 | const resolverRef = useRef<((v: boolean) => void) | null>(null); |
| 2 | const confirm = () => |
| 3 | new Promise<boolean>((resolve) => { |
| 4 | resolverRef.current = resolve; // never cleared on unmount -> leaks the awaiter |
| 5 | }); |
Explanation (EN)
Objašnjenje (HR)
Good example
New codets
| 1 | const resolverRef = useRef<((v: boolean) => void) | null>(null); |
| 2 | const confirm = () => |
| 3 | new Promise<boolean>((resolve) => { |
| 4 | resolverRef.current?.(false); // settle any prior pending promise on re-entry |
| 5 | resolverRef.current = resolve; |
| 6 | }); |
| 7 | useEffect(() => () => resolverRef.current?.(false), []); // settle on unmount |
Explanation (EN)
Objašnjenje (HR)