I have built a app using Tauri (Svelte + Rust). How to make async invoke request from Svelte to Rust such that it dosent freeze the whole UI?

The whole UI freezes when rust function is invoked that calls another async function.

  1. I created a tauri runtime in my main fn()
Main function in main.rs :-

fn main() {
    tauri::Builder::default()
        .invoke_handler(tauri::generate_handler![
            make_type_request_command,
        ])
        .run(tauri::generate_context!())
        .expect("error while running tauri application");
}

Here “make_type_request_command” is an async tauri command function that is calling another async function make_request which returns Result<Value, Box>.

Async Tauri command function :-

#[tauri::command(async)]
async fn make_type_request_command(
    url: &str,
    method: &str,
    headers: &str,
    body: &str,
    request: &str,
) -> Result<Value, String> {
    let result: Result<Value, Box<dyn Error>> = make_request(url, method, headers, body, request).await;
    match result {
        Ok(value) => return Ok(value),
        Err(err) => {
            return Ok(serde_json::Value::String(err.to_string()))
        }
    };
}

Its giving me the error

future cannot be sent between threads safely
the trait `Send` is not implemented for `dyn StdError`".

I have tried a bunch of things already,

  1. Setting #[tokio::main] in make_type_request_command that removes all errors but gives below error at runtime.
thread 'tokio-runtime-worker' 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.'
  1. Added Send + Sync everywhere where error was being returned. Still errored out.

  2. tauri async spawn inside command function.

  3. Making main function async on current thread using tokio.

  4. Remove Box from everywhere possible that is not implementing send.

But nothing worked.

It will be really helpful if someone can help me with this. I want the UI to not freeze until the request is resolved.