Streams
Streams are the asynchronous version of Generators.
They have almost identical next and resume functions, but each must be used
with .await, and we are now allowed to use asynchronous functions inside of
the generator.
async fn send_requests(list) {
let client = http::Client::new();
let do_request = async |url| {
Ok(client.get(url).send().await?.status())
};
for url in list {
yield do_request(url).await;
}
}
let requests = send_requests(["https://google.com", "https://amazon.com"]);
while let Some(status) = requests.next().await.transpose()? {
println!("{}", status);
}
$> cargo run -- run scripts/book/streams/basic_stream.rn
200 OK
200 OK