Handling of while(Runnning) loops

17 views
Skip to first unread message

Thomas Grund

unread,
Apr 24, 2026, 8:52:02 AM (8 days ago) Apr 24
to emscripten-discuss
I have a scripting language implemented in C++. Within the scripting language animation is done in a while loop. The code below is stripped down to a minimal example. It works in Qt by using processEvents instead of emsripten_sleep. For HTML I get

Assertion failed: Cannot have multiple async operations in flight at once

So it is not possible to call runScript twice and I have to break the while loop in the script into iterations and call it repeatedly or use emscripten_set_main_loop? What is the right strategy?

Thanks a lot!
Thomas

counter.cpp

#include <emscripten.h>
#include <emscripten/bind.h>

static int Counter = 100;
static bool Running = false;

int runScript(int Script) {
if (Script==1){ // start loop
Counter = 0;
Running = true;
while (Running) {
Counter++;
emscripten_sleep(100);
}
}
if (Script==2) { // stop loop
Running = false;
}
if (Script==3) { // reset
Counter = 0;
}
if (Script==4) { // get value
return Counter;
}
return 0;
}

EMSCRIPTEN_BINDINGS(counter) {
    emscripten::function("runScript", &runScript);
}

// emcc counter.cpp -o counter.js -sASYNCIFY -lembind -sNO_EXIT_RUNTIME=1 -sSINGLE_FILE


index.html

<!DOCTYPE html>
<html>
<body>
  <p id="display">0</p>
  <button onclick="Module.runScript(1)">Start</button>
  <button onclick="Module.runScript(2)">Stop</button>
  <button onclick="Module.runScript(3)">Reset</button>

  <script>
    var Module = {};
    setInterval(() => {
      if (Module.runScript)
        document.getElementById('display').textContent = Module.runScript(4);
    }, 100);
  </script>
  <script src="counter.js"></script>
</body>
</html>

Sam Clegg

unread,
Apr 24, 2026, 2:04:50 PM (8 days ago) Apr 24
to emscripte...@googlegroups.com
On Fri, Apr 24, 2026 at 5:52 AM Thomas Grund <thomas.g...@gmail.com> wrote:
I have a scripting language implemented in C++. Within the scripting language animation is done in a while loop. The code below is stripped down to a minimal example. It works in Qt by using processEvents instead of emsripten_sleep. For HTML I get

Assertion failed: Cannot have multiple async operations in flight at once

So it is not possible to call runScript twice and I have to break the while loop in the script into iterations and call it repeatedly or use emscripten_set_main_loop? What is the right strategy?

As the assertions says you can only have one suspended operation at a time.  So you can you call `runScript` as many times as you like as long as you wait for the first one to return before running next one.

If the first call to `runScript` is still sleeping then you cannot call `runScript` again.

So you can do something like this.

```
let isRunning = false;
if (!isRunning) {
 isRunning = true;
 Module.runScript().then(() => { isRunner = false });
}
```

i.e. the `runScript` will be exposed as returning a promise and you should not re-enter Wasm until that promise resolves.

cheers,
sam

--
You received this message because you are subscribed to the Google Groups "emscripten-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to emscripten-disc...@googlegroups.com.
To view this discussion visit https://groups.google.com/d/msgid/emscripten-discuss/ddc8e40d-ba87-475c-a981-2e34fbd59e8fn%40googlegroups.com.

Thomas Grund

unread,
Apr 25, 2026, 4:31:25 AM (7 days ago) Apr 25
to emscripten-discuss
Thanks for clarifying, that helps!

Best,
Thomas
Reply all
Reply to author
Forward
0 new messages