Function futures::prelude::future::try_join

source ·
pub fn try_join<Fut1, Fut2>(future1: Fut1, future2: Fut2) -> TryJoin<Fut1, Fut2> 
where Fut1: TryFuture, Fut2: TryFuture<Error = <Fut1 as TryFuture>::Error>,
Expand description

Joins the result of two futures, waiting for them both to complete or for one to produce an error.

This function will return a new future which awaits both futures to complete. If successful, the returned future will finish with a tuple of both results. If unsuccessful, it will complete with the first error encountered.

Note that this function consumes the passed futures and returns a wrapped version of it.

Examples

When used on multiple futures that return Ok, try_join will return Ok of a tuple of the values:

use futures::future;

let a = future::ready(Ok::<i32, i32>(1));
let b = future::ready(Ok::<i32, i32>(2));
let pair = future::try_join(a, b);

assert_eq!(pair.await, Ok((1, 2)));

If one of the futures resolves to an error, try_join will return that error:

use futures::future;

let a = future::ready(Ok::<i32, i32>(1));
let b = future::ready(Err::<i32, i32>(2));
let pair = future::try_join(a, b);

assert_eq!(pair.await, Err(2));