-
let ids = vec![1, 2, 3];
let data = ids
.into_iter()
.map(|x| async {
// httpGetData(id).await
})
.collect(); |
Beta Was this translation helpful? Give feedback.
Answered by
Darksonn
Dec 26, 2021
Replies: 1 comment 2 replies
-
The way to do this is to use a use futures::stream::StreamExt;
async fn http_get_data(id: i32) -> String {
id.to_string()
}
#[tokio::main]
async fn main() {
let ids = vec![1, 2, 3];
let data: Vec<_> = futures::stream::iter(ids)
.then(|id| async move {
http_get_data(id).await
})
.collect()
.await;
println!("{:?}", data);
} Note that the async block is not actually necessary in this particular example: let data: Vec<_> = futures::stream::iter(ids)
.then(|id| http_get_data(id))
.collect()
.await; |
Beta Was this translation helpful? Give feedback.
2 replies
Answer selected by
lz1998
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The way to do this is to use a
Stream
rather than anIterator
. Streams have a method similar tomap
calledthen
which lets you perform async operations.Note that the async block is not actually necessary in this particular example: