Combine un-awaited futures sequentially?

I have 2 futures. I would like to combine them into one future that I can await. Specifically, when the combined future is awaited, I would like to first await the first future and then await the second future.

I do not need the return value of the first future, it can be ignored (insofar as it is not an error). But I do need the return value of the second future.

I can't find a simple way to combine the 2 futures. It looks like the only solution is to use an async closure?

Here's what I've researched & found so far:

  • Stack Overflow Answer 1
    • This seems to be addressing a different problem and iter_ok doesn't seem to exist anymore?
  • There is join, but this polls the futures concurrently (which I guess should have the same end result, but seems less performant since I know I only need to poll the 2nd future after the first one has been completed).
  • and_then seems like what I want, but it only works on awaited futures

The specific thing I'm trying to do is to create a future that encapsulates sending a request through a channel & then waiting for a single response.

I'm not sure what you mean by this. You can call and_then directly on a Future without awaiting it first.

You can also use an async block (which is different from an async closure):

let combined_future = async move {
    future1.await;
    future2.await
};
1 Like

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.