Is it possible to run WebAssembly code async?

后端 未结 2 1282
南方客
南方客 2021-01-11 14:35

I have written a C function that I am able to execute from Angular/TypeScript/JavaScript using WebAssembly:

testWebAssembly() {
    Module.ccall(\"aCFunction         


        
相关标签:
2条回答
  • 2021-01-11 14:42

    WebAssembly and JavaScript share the same execution thread, i.e. when you execute WebAssembly from within JavaScript, your JavaScript code halts, and vice-versa. In that respect it's just like executing any other native (i.e, browser-supplied) APIs from your JavaScript code.

    One option you do have is to run your WebAssembly in a WebWorker, using postMessage to send messages to your wasm code that executed in a different thread. There's a great example here, that renders a fractal using WebWorkers:

    https://www.reddit.com/r/rust/comments/8hdq5r/a_rust_javascript_web_workers_fractal_renderer/

    In the future WebAssembly will likely have its own support for threading:

    https://github.com/WebAssembly/threads

    0 讨论(0)
  • 2021-01-11 14:59

    Asynchronous execution alone won't take it off the main thread. What you actually mean is executing it concurrently. The only way to achieve that on the Web is by using worker threads.

    0 讨论(0)
提交回复
热议问题