3 ms·
Async is another problem detailed that still plagues building wasm programs. If your program has to read a file using fetch the operation is asynchronous. C an
by devwastaken 5y ago
Async is another problem detailed that still plagues building wasm programs.
If your program has to read a file using fetch the operation is asynchronous. C and wasm doesn't understand this. You cannot await a response inside wasm the same as in JS.
The caveat of asyncify is detailed in the docs:
"It is not safe to start an async operation while another is already running. The first must complete before the second begins."[1]
It's a hack, which does work in some instances, but I'm not comfortable putting stack winding hacks into code that's meant to be debuggable. Spooky things could happen.
I'm still looking for alternatives, maybe rust can do it since rust has native async/await support. Haven't quite yet understood what !?!!!?!().?()! Means in rust yet. :)
1 https://emscripten.org/docs/porting/asyncify.html https://emscripten.org/docs/porting/asyncify.html
- oconnor663 5y agoNot sure how much this helps, but in Rust "!" usually means "this name refers to a macro" and "?" usually means "if the value of this expression is an error, short-circuit the current function by returning that error." Usually when you're reading working code, you can ignore those two symbols and still get the meaning. (Those symbols also have some more advanced meanings for when you're dealing with generic/templated types, but those are pretty rare and unlikely to show up in examples.)
- dnautics 5y agoif you are looking for alternatives, you may want to try zig, I think async zig works in wasm, and I think you don't need to put stack winding hacks into the code.
- RReverser 5y agoYeah Rust's wasm-bindgen has integration between Rust async-await and JavaScript promises which doesn't have same restrictions: https://rustwasm.github.io/wasm-bindgen/reference/js-promises-and-rust-futures.html https://rustwasm.github.io/wasm-bindgen/reference/js-promise... However, Asyncify is useful in other scenarios - where you want to shim out an API that is normally synchronous in C / Rust land, with an async JS implementation. async-await, like any coroutine-based mechanism, requires changing every single function in the entire callstack to be async as well, which is usually not feasible in such scenarios (e.g. when shimming out some basic filesystem API). That's where Asyncify helps a lot.