Skip to content

Instantly share code, notes, and snippets.

@LeonAlvarez
Created August 14, 2020 00:05
Show Gist options
  • Save LeonAlvarez/a19f8f04a4d4207988b2e32d4871b336 to your computer and use it in GitHub Desktop.
Save LeonAlvarez/a19f8f04a4d4207988b2e32d4871b336 to your computer and use it in GitHub Desktop.
use futures::{StreamExt};
use std::time::Duration;
use async_std::task;
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error + 'static>> {
let data:Vec<u32> = (0..50).collect();
println!("Waiting...");
let fetches = futures::stream::iter(
data.into_iter().map(|id| {
let x = id.clone();
async move {
let request = async { x };
task::sleep(Duration::from_secs(1)).await;
request.await;
}
})
).buffer_unordered(12).collect::<Vec<()>>();
fetches.await;
println!("Done...");
/*
stream::iter(data).for_each(|id| async move {
let request = async { id }; // async io request
let res = request.await;
println!("res: {:?}", res);
()
}).await;
*/
Ok(())
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment