rust-lang / futures-rs

Zero-cost asynchronous programming in Rust
https://rust-lang.github.io/futures-rs/
Apache License 2.0
5.35k stars 619 forks source link

Question: wait for futures that return no result to complete #2697

Open Enigo opened 1 year ago

Enigo commented 1 year ago

I need to perform ~20k API calls. To speed things up I wanted to execute them in parallel + using buffer_unordered My code looks like this:

pub async fn read() {
    let ids: Vec<i32> = (1..20000).collect();
    let mut futures = futures::stream::iter(ids)
        .map(|id| process_id(id))
        .buffer_unordered(15);

    // waiting for all to complete
    while let Some(_) = futures.next().await {}
}

async fn process_id(id: i32) {
    // API call is here
}

Question - is it an idiomatic way to wait for futures that return no result? I'm referring to this part: while let Some(_) = futures.next().await {}

Thanks!