#How to await inside tauri main_window.listen("tauri://close-requested")
18 messages · Page 1 of 1 (latest)
if you mean awaiting its completion and/or getting back a value that you want to use in the rest of the listen closure then i think this is not possible 🤔
or actually, nevermind. Since the listen closure is a callback anyway you could turn the whole closure into an async function
Well, I need to listen to this event in rust side
ohhh, you're talking about the rust side
nevermind then
tauri://close-requested is javascript only, on rust you need to use the .on_window_event() or .run()
and then you can use async_runtime::block_on(async_task) to run the async function.
I use tokio and got this error
thread 'main' panicked at 'Cannot start a runtime from within a runtime. This happens because a function (like `block_on`) attempted to block the current thread while the thread is being used to drive asynchronous tasks.', /Users/user/.cargo/registry/src/github.com-1ecc6299db9ec823/tauri-1.2.4/src/async_runtime.rs:208:27
I think I need to use tokio::spawn instead of async_runtime::block_on
am I right ?
spawn won't wait for the task to finish so the app may close before its done
are you using #[tokio::main]?
because normally it shouldn't throw that error in this context
<#1009605599673196555 message>
Yes, I use #[tokio::main]
And I share the current runtime with Tauri via
tauri::async_runtime::set(tokio::runtime::Handle::current());
I used
tokio::task::block_in_place(move || {
tokio::runtime::Handle::current().block_on(async move {
// Code here
})
});
Without any issue