I have a list of async
functions that I want to execute concurrently and then wait for all them to finish. The working code I have right now is:
async fn start_consumers(&self) {
for consumer in &self.consumers {
consumer.consume().await;
}
}
This is not quite accurate as the functions are executed serially. I am looking for something like join!
, but which works on a dynamic vector. With that I should be able to write something like:
async fn start_consumers(&self) {
let mut v = Vec::new();
for consumer in &self.consumers {
consumer.consume();
}
join!(v);
}
Right now join!
supports only tuples. I am looking for an alternative for that. Something similar to Promise.all()
in JavaScript.
An alternative way to wait for all promises to be done, you have to use async and await keywords, just add async keyword before function checkPhotos(... and ads await keyword before Promise. all... , that's will tell js engine that you need to run the code below await line when you got the response.
async / . await is Rust's built-in tool for writing asynchronous functions that look like synchronous code. async transforms a block of code into a state machine that implements a trait called Future .
async / . await are special pieces of Rust syntax that make it possible to yield control of the current thread rather than blocking, allowing other code to make progress while waiting on an operation to complete.
The keyword await is used to wait for a Promise. It can only be used inside an async function. This keyword makes JavaScript wait until that promise settles and returns its result. Here is an example with a promise that resolves in 2 seconds.
But in Rust, invoking an async function does not do any scheduling, and here the main thing you’ll notice is that futures feel “lazy”: they won’t do anything until you await them inside the async block. After awaiting the Future then the Future executor comes into the picture.
Then, we create two examples of asynchronous functions that use async fn and await. The first function is retrieve_val, whose purpose is to return the value numeric value five after ten seconds. The other asynchronous function is pre_retrieval, which calls the retrieval_val function.
In general, async / await lets you write code that avoids "callback hell", in favor of a linear style similar to blocking code while still letting other tasks progress during awaits. Any minimal async / await code in Rust is made up of at least the following pieces: A Future with an internal state machine
As you all know the async–await syntax hits stable Rust, as part of the 1.39.0 release. So let’s leverage this stabilized feature to make our code-bases Asynchronous with Zero-cost futures.
The futures crate has a join_all
function which allows for waiting on a collection of futures:
use futures::future::join_all;
async fn start_consumers(&self) {
let mut v = Vec::new();
for consumer in &self.consumers {
v.push(consumer.consume());
}
join_all(v).await;
}
I also asked a similar question on the same day, but in my case I had a Result
wrapped in a Future
. So instead of join_all
I had to use try_join_all
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With