PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Friday, September 30, 2022

[FIXED] How to wait for a list of async function calls in Rust?

 September 30, 2022     async-await, concurrency, rust     No comments   

Issue

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.


Solution

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



Answered By - WBuck
Answer Checked By - Terry (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing