Create multiple futures?

looking at the docs it seems that multiple futures are created via FuturesOrdered (or unordered) - and then pushing to that.

But when I try this I get an error:

let foo:FuturesOrdered<Future<Output=Result<String, String>>> = FuturesOrdered::new();

Any tips for the right way to create a bunch of futures that resolve as one future?

I’m using 0.3, and ideally I’d like to ultimately say let foo_results = foo.await?;

A typical type might be;

let foo:FuturesOrdered<Box<dyn Future<...

This is a stream type rather than just Future. Can be used like;

while Some(s) = { ... }

Your more likely after join_all. (contains example.)

1 Like

I think you’re right, that I am really after join_all - thanks!

Followup question - this doesn’t seem to support early return?

In other words if Output=Result<String, Error> - I still need to await on all of them to finish resolving, and then I will get a Vec<Result<String, Error>> which I can then into_iter().collect() to turn into a Result<Vec<String>, Error> … but it will only do this when all of the futures have finished?

What I would ideally like is to get that Result<Vec<String>, Error> - but if one of the futures rejects, it should immediately yield that error since there’s no reason to keep processing the others.

(and at a glance this isn’t directly possible via join_all() since it returns a Vec, not a Result)

Sounds like you’re looking for try_join_all instead. Generally if you want something that has Result specific handling in futures you’ll find it with a try_ prefix on the normal function/method (there are still some missing, so if you notice there’s a function/method you would expect to have a Result specific variant and can’t find it in the docs please open an issue).

1 Like