Four modules open the warung in the morning: a top-level await in harga.js or the old async IIFE, Bagas slow back from the market, and two modules importing…
Why it matters
Useful for setup code: reading config, opening a connection, loading WASM, picking an implementation by what the browser can do, all guaranteed done before another module uses it. But because importers are held back too, one slow await in a widely used module can hold up the whole app’s startup, and two modules waiting on each other can hang with no error.
The key idea
Top-level await makes the module async, and every importer waits with it
Common mistakes
A SyntaxError writing top-level await in a CommonJS file Top-level await only exists in ES modules (type: "module", .mjs, or <script type="module">, an ES2022+ target). In CommonJS or a classic script it is a SyntaxError.
Try it in this playground
Watch kasir.js held back for harga.js Without a single await in kasir.js.
lampu.js does not wait It runs before kasir.js, though it is imported after it.
The old way: the till reads undefined Pick the old IIFE for harga.js.
A circular import: stuck with no error Turn on the circular import, then run it.