crossbind
GitHub

Worker and page thread

A browser app runs the module in a Web Worker unless it asks otherwise. That decides what a call returns and whether the page waits.

The playground runs the module on the page's own thread. While slowSum runs, nothing else on the page does: the timer in main.js never ticks. In a worker the same call returns a promise at once and the page stays free.

That is why every call in this tutorial has an await. initNative({ useWorker: false }) keeps the module on the page thread, where calls return values and callbacks can be passed; see Worker mode.

Try it
Take the await off slowSum and print sum. Here it is still a number; in a worker it would be a promise.
Open in playground
Runs without a compile until the C++, Rust, packages or imports change.
Press Run, or Ctrl+Enter, to run main.js.
Type to search every guide page and section.
↑↓ navigate↵ openesc close