#How do you implement tauri async commands?

27 messages · Page 1 of 1 (latest)

polar quest
#

Hey!
I'm trying to create an async command with Tauri, but whenever I use invoke_handler I get this error. There's probably something I'm missing, any ideas?

error[E0599]: the method `async_kind` exists for reference `&impl Future<Output = Result<String, Box<dyn Error>>>`, but its trait bounds were not satisfied
  --> src/main.rs:13:1
   |
13 | #[tauri::command]
   | ^^^^^^^^^^^^^^^^^ method cannot be called due to unsatisfied trait bounds
...
38 |         .invoke_handler(tauri::generate_handler![test])
   |                         ------------------------------- in this macro invocation
   |
#

Code:

#[tauri::command]
async fn test(name: &str) -> Result<String, ()> {
    Ok(format!("{}", name))
}

fn main() {
    tauri::Builder::default()
        .invoke_handler(tauri::generate_handler![test])
        .run(tauri::generate_context!())
        .expect("error while running tauri application");
worldly marten
#

It's related to ownership and serde

polar quest
worldly marten
#

It's the dezerialization to input to the function part that's unsatsfied, not the return

polar quest
#

and it still returns the same error somehow...

worldly marten
#

That shouldn't be the case

#

Lets see more code

polar quest
#

with or without (async) in #[tauri::command]

polar quest
worldly marten
#

I never have (async) in my commands

#

I wanna say (async) is something we added early on before we made the command automatically detect it was decorating an async fn

polar quest
worldly marten
#

The only explanation I have off the top of my head it that this is wrong
Box<dyn std::error::Error>
Not sure why it'd be wrong, it's 1:14 here and my brain no work good
You shouldn't be running dotenv on every command invocation, just once in the .setup(|app|{}) function
And it should be behind a #{cfg(dev)] condition since dotenv is a development tool, not something you should use in production

#

What's the output of tauri info?

polar quest
# worldly marten What's the output of `tauri info`?
[✔] Environment
    - OS: Fedora 39.0.0 X64
    ✔ webkit2gtk-4.0: 2.44.0
    ✔ rsvg2: 2.57.1
    ✔ rustc: 1.76.0 (07dca489a 2024-02-04)
    ✔ cargo: 1.76.0 (c84b36747 2024-01-18)
    ✔ rustup: 1.27.0 (bbb9276d2 2024-03-08)
    ✔ Rust toolchain: stable-aarch64-unknown-linux-gnu (default)
    - node: 21.7.1
    - yarn: 1.22.21
    - npm: 10.5.0
    - bun: 1.1.3

[-] Packages
    - tauri [RUST]: 1.6.1
    - tauri-build [RUST]: 1.5.1
    - wry [RUST]: 0.24.7
    - tao [RUST]: 0.16.8
    - @tauri-apps/api : not installed!
    - @tauri-apps/cli [NPM]: 1.5.11

[-] App
    - build-type: bundle
    - CSP: unset
    - distDir: ../src
    - devPath: ../src
worldly marten
polar quest
polar quest
worldly marten
#

Try changing the box error to just () and throw away the errors instead

mellow egret
worldly marten
#

That sounds like a better idea 😄

mellow egret
#

I'm not sure that's the same version though because the import locations look a bit different.

#

For example, rs_openai::chat::ChatCompletionMessageRequestBuilder in your code is rs_openai::apis::chat::ChatCompletionMessageRequestBuilder in the docs.

polar quest
#

Yep... the error handling was the problem after all.
Thanks a lot, there are still a few quirks, but I think I can go on from here. Cheers and kudos for all your work here!