#How do you handle async code in sync functions?

12 messages · Page 1 of 1 (latest)

prime ermine
#

Some background info:
I'm targeting both Web & Native.
In using egui & eframe (template: https://github.com/emilk/eframe_template/blob/main/src/main.rs)
Eframe has a App trait to implement for a egui application.
https://docs.rs/eframe/latest/eframe/trait.App.html
In the ui trait function you can call the egui functions to render the Ui.

However they're all sync functions, how can you call a async functions in sync?
My use case is networking with Iroh. Most functions are async.

sterile yacht
#

do you have an async runtime yet?

#

I guess iroh uses tokio. You can use Handle::block_on to run a future synchronously. Or you can spawn it as a task and do something later when it is finished. I'm not sure if there's any complications with doing this on web.

green phoenix
#

All networking in web is always async?? However for linux you'll want something that sets noblocking, I'm sure windows is similar. web also has only a single thread, unless you setup a worker but that's another task not a thread as you'd typically understand. So... I would write all the networking code twice.

#

...or use a library like tungstenite.

vivid nova
prime ermine
prime ermine
vivid nova
#

indeed

#

you can kind of get away with local file io because it's usually fast enough™ , but you still shouldn't

#

in the year of our crab 2026 ms explorer still chokes on slow network shares

#

because they did the same mistake