OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

Cannot await for a function in an async function of Tauri::command

  • Thread starter Thread starter Hashir Shahzad
  • Start date Start date
H

Hashir Shahzad

Guest
I cant await for a function if i use a Tauri function. I am really new to RUST and dont know much about futures and stuff, My program worked well in the terminal but now i just want to make a good UI so i am using tauri, it does not allow me to call an async function

Main.rs​


Code:
#[tauri::command(rename_all = "snake_case")]
async fn url_entered(url: &str, img_box_checked:bool, pdf_box_checked:bool, subfolder_box_checked:bool) -> Result<String, ()> {
    println!("IMAGE:{} | PDF:{} | Folder:{} | URL: {}", img_box_checked, pdf_box_checked, subfolder_box_checked, url);


    let _ = get_table(url, CURRENT_DIRECTORY, pdf_box_checked, img_box_checked, subfolder_box_checked).await; // Await here causes error

    Ok(format!("Sending request to: {}", url))
}

Main.js​


Code:
const { invoke } = window.__TAURI__.tauri;

let urlInputEl;
let urlMsgEl;
let downloadPdfs;
let downloadImages;
let scanSubfolders;
async function url_submitted() {
  urlMsgEl.textContent = "Trying to download...";
  // Learn more about Tauri commands at https://tauri.app/v1/guides/features/command
  const { url, img_box_checked, pdf_box_checked, subfolder_box_checked } = {
    url: urlInputEl.value,
    img_box_checked: downloadImages.checked,
    pdf_box_checked: downloadPdfs.checked,
    subfolder_box_checked: scanSubfolders.checked
  };

  urlMsgEl.textContent = await invoke("url_entered", { url, img_box_checked, pdf_box_checked, subfolder_box_checked }).then(() =>
    console.log('Completed!'));
}

window.addEventListener("DOMContentLoaded", () => {

  urlInputEl = document.querySelector("#url-input");
  urlMsgEl = document.querySelector("#url-msg");
  downloadPdfs = document.querySelector("#DownloadPdfs");
  downloadImages = document.querySelector("#DownloadImages");
  scanSubfolders = document.querySelector("#ScanSubfolders");

  document.querySelector("#url-form").addEventListener("submit", (e) => {
    e.preventDefault();
    url_submitted();
  });
});

There is a button in html:

Code:
<button type="submit">Enter</button>

Code:
error: future cannot be sent between threads safely
   --> src\main.rs:214:1
    |
214 | #[tauri::command(rename_all = "snake_case")]
    | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ future returned by `url_entered` is not `Send`
...
231 |         .invoke_handler(tauri::generate_handler![url_entered])
    |                         ------------------------------------- in this macro invocation
    |
    = help: within `ego_tree::Node<scraper::Node>`, the trait `Sync` is not implemented for `std::cell::Cell<NonZero<usize>>`, which is required by `impl futures::Future<Output = Result<String, ()>>: std::marker::Send`
    = note: if you want to do aliasing and mutation between multiple threads, use `std::sync::RwLock`
note: future is not `Send` as this value is used across an await
   --> src\main.rs:89:90
    |
35  |     for table in html.select(&table_selector){ // get all tables from html
    |         ----- has type `ElementRef<'_>` which is not `Send`
...
89  |                                          download_pdfs, download_imgs, scan_subfolders)).await?; // Call get_table function with new url
    |                                                                                          ^^^^^ await occurs here, with `table` maybe used later
note: required by a bound in `ResultFutureTag::future`
   --> C:\Users\DELL\.cargo\registry\src\index.crates.io-6f17d22bba15001f\tauri-1.6.8\src\command.rs:293:42
    |
289 |     pub fn future<T, E, F>(self, value: F) -> impl Future<Output = Result<Value, InvokeError>>
    |            ------ required by a bound in this associated function
...
293 |       F: Future<Output = Result<T, E>> + Send,
    |                                          ^^^^ required by this bound in `ResultFutureTag::future`
    = note: this error originates in the macro `__cmd__url_entered` which comes from the expansion of the macro `tauri::generate_handler` (in Nightly builds, run with -Z macro-backtrace for more info)

I tried searching on google and use codeium but nothing worked.

<p>I cant await for a function if i use a Tauri function.
I am really new to RUST and dont know much about futures and stuff, My program worked well in the terminal but now i just want to make a good UI so i am using tauri, it does not allow me to call an async function</p>
<h3>Main.rs</h3>
<pre class="lang-rust prettyprint-override"><code>#[tauri::command(rename_all = "snake_case")]
async fn url_entered(url: &str, img_box_checked:bool, pdf_box_checked:bool, subfolder_box_checked:bool) -> Result<String, ()> {
println!("IMAGE:{} | PDF:{} | Folder:{} | URL: {}", img_box_checked, pdf_box_checked, subfolder_box_checked, url);


let _ = get_table(url, CURRENT_DIRECTORY, pdf_box_checked, img_box_checked, subfolder_box_checked).await; // Await here causes error

Ok(format!("Sending request to: {}", url))
}
</code></pre>
<h3>Main.js</h3>
<pre class="lang-javascript prettyprint-override"><code>const { invoke } = window.__TAURI__.tauri;

let urlInputEl;
let urlMsgEl;
let downloadPdfs;
let downloadImages;
let scanSubfolders;
async function url_submitted() {
urlMsgEl.textContent = "Trying to download...";
// Learn more about Tauri commands at https://tauri.app/v1/guides/features/command
const { url, img_box_checked, pdf_box_checked, subfolder_box_checked } = {
url: urlInputEl.value,
img_box_checked: downloadImages.checked,
pdf_box_checked: downloadPdfs.checked,
subfolder_box_checked: scanSubfolders.checked
};

urlMsgEl.textContent = await invoke("url_entered", { url, img_box_checked, pdf_box_checked, subfolder_box_checked }).then(() =>
console.log('Completed!'));
}

window.addEventListener("DOMContentLoaded", () => {

urlInputEl = document.querySelector("#url-input");
urlMsgEl = document.querySelector("#url-msg");
downloadPdfs = document.querySelector("#DownloadPdfs");
downloadImages = document.querySelector("#DownloadImages");
scanSubfolders = document.querySelector("#ScanSubfolders");

document.querySelector("#url-form").addEventListener("submit", (e) => {
e.preventDefault();
url_submitted();
});
});
</code></pre>
<p>There is a button in html:</p>
<pre class="lang-html prettyprint-override"><code><button type="submit">Enter</button>
</code></pre>
<pre><code>error: future cannot be sent between threads safely
--> src\main.rs:214:1
|
214 | #[tauri::command(rename_all = "snake_case")]
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ future returned by `url_entered` is not `Send`
...
231 | .invoke_handler(tauri::generate_handler![url_entered])
| ------------------------------------- in this macro invocation
|
= help: within `ego_tree::Node<scraper::Node>`, the trait `Sync` is not implemented for `std::cell::Cell<NonZero<usize>>`, which is required by `impl futures::Future<Output = Result<String, ()>>: std::marker::Send`
= note: if you want to do aliasing and mutation between multiple threads, use `std::sync::RwLock`
note: future is not `Send` as this value is used across an await
--> src\main.rs:89:90
|
35 | for table in html.select(&table_selector){ // get all tables from html
| ----- has type `ElementRef<'_>` which is not `Send`
...
89 | download_pdfs, download_imgs, scan_subfolders)).await?; // Call get_table function with new url
| ^^^^^ await occurs here, with `table` maybe used later
note: required by a bound in `ResultFutureTag::future`
--> C:\Users\DELL\.cargo\registry\src\index.crates.io-6f17d22bba15001f\tauri-1.6.8\src\command.rs:293:42
|
289 | pub fn future<T, E, F>(self, value: F) -> impl Future<Output = Result<Value, InvokeError>>
| ------ required by a bound in this associated function
...
293 | F: Future<Output = Result<T, E>> + Send,
| ^^^^ required by this bound in `ResultFutureTag::future`
= note: this error originates in the macro `__cmd__url_entered` which comes from the expansion of the macro `tauri::generate_handler` (in Nightly builds, run with -Z macro-backtrace for more info)

</code></pre>
<p>I tried searching on google and use codeium but nothing worked.</p>
 
Top