Triggering async function on button click #634
-
I want to trigger a download when I click on a button, but the download function is async using tokio. I have something like this inside the
But I'm getting the error that it must be inside an async block. |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 16 replies
-
If you want the application to stop responding until download completes, you can use spawn blocking method of tokio which will download the links but hang until it completes. But a better way would be to send the downloads to tokio which should be running in another thread. Tokio recently updated the guide for this purpose. https://tokio.rs/tokio/topics/bridging You spawn tokio on another thread, and use the handle or a channel to send tasks like download video to tokio. The main thread and event loop will continue normally because downloads are happening in another thread. |
Beta Was this translation helpful? Give feedback.
-
I made a simple example of triggering a concurrent download using the If you are using |
Beta Was this translation helpful? Give feedback.
-
It seems like threads/tasks are not supported on wasm/web yet, available only for native. I tried both spawning threads/tasks, didn't work: If anyone knows how to do so on wasm, your reply would be much appreciated! Edit: |
Beta Was this translation helpful? Give feedback.
If you want the application to stop responding until download completes, you can use spawn blocking method of tokio which will download the links but hang until it completes. But a better way would be to send the downloads to tokio which should be running in another thread. Tokio recently updated the guide for this purpose. https://tokio.rs/tokio/topics/bridging
You spawn tokio on another thread, and use the handle or a channel to send tasks like download video to tokio. The main thread and event loop will continue normally because downloads are happening in another thread.