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).
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)
Wrapping everything in main() and calling it adds an indirection with no benefit in an ESM module that already supports top-level await.
Objašnjenje (HR)
Omotavanje svega u main() i njegov poziv dodaje indirekciju bez koristi u ESM modulu koji vec podrzava top-level await.
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)
A main() wrapper still makes sense if you genuinely need to export the entry function for reuse/testing, or to share a single try/catch around the whole flow.
Bilješke (HR)
main() omotac i dalje ima smisla ako stvarno trebas exportati entry funkciju za ponovnu upotrebu/testiranje, ili dijeliti jedan try/catch oko cijelog toka.
Exceptions / Tradeoffs (EN)
Keep an explicit entry function when it must be importable/testable or when targeting a CommonJS module without top-level await.
Iznimke / Tradeoffi (HR)
Zadrzi eksplicitnu entry funkciju kad mora biti importabilna/testabilna ili kad ciljas CommonJS modul bez top-level awaita.