I have a computationally intensive WASM C function that uses ASYNCIFY to send status updates to the DOM:
EM_JS(void, updateStatus, (int number), {
Asyncify.handleAsync(async() => {
await Promise.resolve()
.then(function() {
console.log("status: ", number);
document.getElementById("status").innerHTML = "Status: " + number;
});
await new Promise(r => setTimeout(r, 50));
});
});
int myWASM(int param) {
printf("Running myWASM()\n");
...
for(int number = 0; number < MAX; number++) {
...
updateStatus(number);
...
}
...
printf("done with myWASM()\n");
return 0;
}
The Javascript caller is equivalent to:
var startTime = performance.now();
var callMyWASM = cwrap("myWASM", "number", ["number"]);
var ret = callMyWASM(arg);
var totalTime = performance.now() - startTime;
console.log("myWASM ran in ", totalTime, "ms and returned", ret);
Strangely, the console reads:
Running myWASM()
myWASM ran in 10166.440000000875 ms and returned 0
done with myWASM()
And indeed, the DOM updates continue even after the Javascript reports that myWASM returned. I assume myWASM() is now an asyncronous function, and I wonder if there's a way to await or similar until it finishes before proceeding?
Edit: Following up on @Bergi's suggestion, changing myWASM() to return 43 results in the same output: Javascript gets a return value of 0 regardless.
returna promise (or call a callback, and then promisify in JS) from your wasm function.EM_JS updateStatus()timeout executes.