How to use "await?" inside vec.iter().map() in an async fn?

If you have a small number of elements then join_all(iter.map(|x| async { can.await here }) will work.

If the number of elements is huge, then I suggest using a stream from iterator and buffer_unordered + collect to ensure too many of them aren't processed at the same time. It is a bit fiddly, since some stream methods expect returning async blocks, and some not.

4 Likes