rustrust-tokiorust-async-std

How to fix select_all returning only one result instead of multiple results


My code can be boiled down to the following:

let client = reqwest::Client::new();
let endpoints = vec!['http://google.com', 'http://www.yahoo.com', 'http://example.com'];
let futures: Vec<_> = endpoints.iter().map(|endpoint| {
            return client
                .get(endpoint)).send();
}).collect();

let (item_resolved, _remaining_futures, _last) = select_all(futures).await;
let mut responses = vec![];

for item in item_resolved {
    responses.push(item.json::<ResponseStruct>().await);
}

dbg!(responses);

problem is responses only contain one result. I expect it to be a vec of results from calling the endpoints.


Solution

  • This can be achieved with FuturesUnordered from the futures crate.

    Here's an example I just shared from another answer: https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=7f2faf54cec2267b7c6b0839439dea0d