Run list of futures sequentially in-order, one-by-one

Hello everybody,

I have an ordered list of futures that I like run sequentially in-order, one-by-one and until all futures have been successfully executed or an error is encountered. Generally speaking, I’m looking for a way to automatically and_then all my futures from the list.

My use case is to create a list of todos items via REST calls that preserve the order the items were created. It makes sense to create a todo item n if the item n-1 has been successfully created.

So far, the only hint I found is StackOverflow question, but – as far as I understand – the solution does not take the futures’s results into account.

I hope somebody has an idea, because I don’t know how to approach this other than writing a macro to actually .and_then the futures. But there must be better way.

Thanks in advance!

In case of error, do you need access to the accumulated results (ie previous futures’ results)?

There are several combinators that process streams in order and only continue if there’s no error. However, the ones that accumulate results will drop them when an error occurs. So depending on the above, they may not work for you.

There are also combinators like loop_fn where you can control the behavior more precisely. You can also write your own Future impl that provides the precise behavior you want - it wouldn’t be too involved. It sounds like it might be similar to the Collect one except you would yield accumulated results on error.

Wait 6 months for await is easy solution.

Also checking your aware futures start running as soon as they are created. They just get blocked when a call to poll is needed. lazy being way to wrap their creation in a closure so start is delayed.

Not really in Rust, they may synchronously do some work during construction, but unless they are spawning work off into the background (via a thread pool or by giving some work to the kernel) they literally cannot do anything if you don't call poll. As far as I'm aware that's not a common thing to do, and even simple adaptors like write_all won't do anything until the first poll.