Rules Hub
Code Ruleset
Rule priority, scope & exceptions
Use this to align rules with the senior-level structure (P0/P1/P2, scope, exceptions/tradeoffs).
Use top-level await instead of a main() wrapper in ESM scripts
ESM supports top-level await; run entry logic at the top level rather than wrapping it in main() and immediately invoking it.
Bad example
| 1 | async function main() { |
| 2 | const args = parseCliArgs(process.argv.slice(2)); |
| 3 | const data = await fetchData(args); |
| 4 | await process(data); |
| 5 | } |
| 6 |
|
| 7 | await main(); |
Explanation (EN)
A wrapper used only once may add indirection when it serves no lifecycle or reuse purpose.
Objašnjenje (HR)
Omotač korišten samo jednom može dodati nepotrebnu razinu kada nema svrhu za životni ciklus ili ponovnu uporabu.
Good example
| 1 | const args = parseCliArgs(process.argv.slice(2)); |
| 2 | const data = await fetchData(args); |
| 3 | await process(data); |
Explanation (EN)
Running the logic at the top level is more explicit about what executes immediately and removes a needless function boundary.
Objašnjenje (HR)
Pokretanje logike na top-levelu jasnije pokazuje sto se odmah izvrsava i uklanja nepotrebnu granicu funkcije.
Notes (EN)
Use top-level await in a true ESM entry point when its runtime/build target supports it and importing the module is intended to execute the work.
Bilješke (HR)
Top-level await koristi u stvarnoj ESM ulaznoj točki kada ga runtime i build podržavaju te import namjerno treba izvršiti taj posao.
Exceptions / Tradeoffs (EN)
Keep an explicit entry function for importable tests, reusable flows, CommonJS compatibility, shared error handling or controlled startup. Top-level await delays dependent module evaluation.
Iznimke / Tradeoffi (HR)
Zadrži ulaznu funkciju za testove koji je importaju, ponovnu uporabu, CommonJS kompatibilnost, zajedničku obradu pogreške ili kontrolirano pokretanje. Top-level await odgađa evaluaciju ovisnih modula.