Rules Hub
Coding Rules Library
Rule priority, scope & exceptions
Use this to align rules with the senior-level structure (P0/P1/P2, scope, exceptions/tradeoffs).
Keep a conditional fetch inside Promise.all by resolving to an empty value
When one of several parallel fetches is optional, replace it with Promise.resolve(empty) inside the same Promise.all so the destructured tuple stays positional.
Bad example
| 1 | const [rows, currencies, fxRows] = await Promise.all([ |
| 2 | this.mda.fetchPriceHistory(insrefs, from, to), |
| 3 | this.mda.fetchCurrencies(insrefs), |
| 4 | this.mda.fetchFxRates(fxInsrefs, from, to), |
| 5 | ]); |
| 6 | // optional fetch pulled out -> extra serial await, breaks the parallel batch |
| 7 | let currentPrices = new Map(); |
| 8 | if (includeToday) currentPrices = await this.mda.fetchCurrentPrices([...insrefs, ...fxInsrefs]); |
Explanation (EN)
Pulling the optional fetch out of Promise.all adds a serial await after the batch (losing parallelism) and scatters the result handling across two statements.
Objašnjenje (HR)
Izvlacenje opcionalnog dohvata iz Promise.all dodaje serijski await nakon grupe (gubi se paralelizam) i rasipa obradu rezultata na dvije naredbe.
Good example
| 1 | const optionalInsrefs = includeToday ? [...insrefs, ...fxInsrefs] : []; |
| 2 | const [rows, currencies, fxRows, currentPrices] = await Promise.all([ |
| 3 | this.mda.fetchPriceHistory(insrefs, from, to), |
| 4 | this.mda.fetchCurrencies(insrefs), |
| 5 | this.mda.fetchFxRates(fxInsrefs, from, to), |
| 6 | optionalInsrefs.length > 0 |
| 7 | ? this.mda.fetchCurrentPrices(optionalInsrefs) |
| 8 | : Promise.resolve(new Map<number, number | null>()), |
| 9 | ]); |
Explanation (EN)
Resolving the skipped branch to an empty value keeps the batch fully parallel and the destructured tuple positional, so downstream code needs no extra null-handling.
Objašnjenje (HR)
Razrjesavanje preskocene grane praznom vrijednoscu zadrzava grupu potpuno paralelnom i tuple pozicijskim, pa downstream kod ne treba dodatno rukovanje null vrijednostima.
Exceptions / Tradeoffs (EN)
If the optional call is expensive even when its input is empty, or its absence should change control flow, branch explicitly instead.
Iznimke / Tradeoffi (HR)
Ako je opcionalni poziv skup i kad mu je ulaz prazan, ili njegovo izostajanje treba promijeniti tok upravljanja, granaj eksplicitno umjesto toga.