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).
Don't await synchronous, non-Promise values
Awaiting a value that isn't a Promise is a no-op; drop the await or make the callee return a Promise.
Bad example
| 1 | // runJob returns void, not a Promise |
| 2 | function runJob(name: string): void { |
| 3 | doWork(name); |
| 4 | } |
| 5 |
|
| 6 | async function dispatch(name: string): Promise<void> { |
| 7 | await runJob(name); // await does nothing here |
| 8 | } |
Explanation (EN)
await on a synchronous value resolves it immediately and yields the same value; it does no real waiting. It misleads readers into thinking runJob is asynchronous and adds an unnecessary microtask.
Objašnjenje (HR)
await nad sinkronom vrijednoscu odmah je razrjesava i vraca istu vrijednost; nema stvarnog cekanja. Zavarava citatelja da je runJob asinkrona i dodaje nepotreban microtask.
Good example
| 1 | // Either call it synchronously... |
| 2 | function dispatch(name: string): void { |
| 3 | runJob(name); |
| 4 | } |
| 5 |
|
| 6 | // ...or make the callee genuinely async and await it. |
| 7 | async function runJob(name: string): Promise<void> { |
| 8 | await doWork(name); |
| 9 | } |
| 10 | async function dispatch(name: string): Promise<void> { |
| 11 | await runJob(name); |
| 12 | } |
Explanation (EN)
Match await usage to the actual return type: call synchronous functions directly, and only await functions that truly return a Promise. The code then communicates real async boundaries.
Objašnjenje (HR)
Uskladi koristenje await-a sa stvarnim povratnim tipom: sinkrone funkcije zovi izravno, a await koristi samo za funkcije koje stvarno vracaju Promise. Tako kod jasno pokazuje stvarne asinkrone granice.
Notes (EN)
Avoid leaving justifying comments like 'awaiting a sync function is harmless' — remove the await instead. TypeScript's await-thenable lint rule flags this.
Bilješke (HR)
Izbjegavaj opravdavajuce komentare tipa 'await nad sinkronom funkcijom je bezopasan' — radije ukloni await. TypeScript-ovo await-thenable lint pravilo ovo oznacava.
Exceptions / Tradeoffs (EN)
Awaiting a possibly-thenable value of unknown type (e.g. a return typed as `T | Promise<T>`) is legitimate, since await normalizes both. The rule targets values known to be synchronous.
Iznimke / Tradeoffi (HR)
Await nad vrijednoscu koja mozda jest thenable, a nepoznatog je tipa (npr. povrat tipiziran kao `T | Promise<T>`), je legitiman jer await normalizira oba slucaja. Pravilo cilja vrijednosti za koje znamo da su sinkrone.