#How to await inside tauri main_window.listen("tauri://close-requested")

18 messages · Page 1 of 1 (latest)

torpid ice
#

How to run async fn and await them inside tauri listen closure ?

open breach
#

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

torpid ice
open breach
#

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.

torpid ice
#

I think I need to use tokio::spawn instead of async_runtime::block_on

#

am I right ?

open breach
#

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>

torpid ice
#

Yes, I use #[tokio::main]

#

And I share the current runtime with Tauri via
tauri::async_runtime::set(tokio::runtime::Handle::current());

torpid ice
#

I used

tokio::task::block_in_place(move || {
    tokio::runtime::Handle::current().block_on(async move {
        // Code here
    })
});

Without any issue