So I implmented this code-example and changed it up a little. It now looks like this:
pub async fn loadAllSNPData(&mut self, client: &Client) {
let bodies = stream::iter(&self.snps)
.map(|snp| {
let url = format!("https://ncbi.gov/?db=snp&id={}",snp.identifier);
let client = &client;
async move {
let resp = client.get(&url).send().await?;
let rawJson = resp.text().await;
// parse json
let json: Value = serde_json::from_str(&rawJson).expect("JSON was not well-formatted");
Ok((json,snp))
}
})
.buffer_unordered(PARALLEL_REQUESTS);
bodies
.for_each(|b| {
async {
match b {
Ok(b) => println!("Got {} bytes", b),
Err(e) => eprintln!("Got an error: {}", e),
}
}
})
.await;
}
It was all working fine until I wanted to return something different than the rawJson
-Variable containing the text. I want to return the parsed json and the according snp-element to another function that then filters some information from the JSON-Object and stored it inside the corresponding snp-Object. But whenever I change the object that gets returned I get the following error:
error[E0277]: the
?
operator can only be used in an async block that returnsResult
orOption
...
It then continues to mark the entire async move
-Block as the origin of the error. How would I go about this to return different things?
Edit: I now return Ok((json,snp)) and get
error[E0698]: type inside
async fn
body must be known in this context
Ok((json,snp))
^^ cannot infer type for type parameter `E` declared on
the enum `Result`