-
I can retry functions like:
But how can I retry a function with a parameter like:
|
Beta Was this translation helpful? Give feedback.
Answered by
Xuanwo
May 6, 2024
Replies: 1 comment
-
Sadly, we don't have direct support for async func which has params (due to the limit of rust itself). The only possible way is to capture as a closure: use anyhow::Result;
use backon::ExponentialBuilder;
use backon::Retryable;
async fn fetch(url: &str) -> Result<String> {
Ok(reqwest::get(url).await?.text().await?)
}
#[tokio::main(flavor = "current_thread")]
async fn main() -> Result<()> {
let content = (|| async { fetch("https://www.rust-lang.org").await })
.retry(&ExponentialBuilder::default())
.when(|e| e.to_string() == "retryable")
.await?;
println!("fetch succeeded: {}", content);
Ok(())
} |
Beta Was this translation helpful? Give feedback.
0 replies
Answer selected by
Xuanwo
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Sadly, we don't have direct support for async func which has params (due to the limit of rust itself). The only possible way is to capture as a closure: