Macro futures_util::try_join
source · [−]macro_rules! try_join {
($($tokens:tt)*) => { ... };
}
Expand description
Polls multiple futures simultaneously, resolving to a Result
containing
either a tuple of the successful outputs or an error.
try_join!
is similar to join!
, but completes immediately if any of
the futures return an error.
This macro is only usable inside of async functions, closures, and blocks.
It is also gated behind the async-await
feature of this library, which is
activated by default.
Examples
When used on multiple futures that return Ok
, try_join!
will return
Ok
of a tuple of the values:
use futures::try_join;
let a = async { Ok::<i32, i32>(1) };
let b = async { Ok::<i32, i32>(2) };
assert_eq!(try_join!(a, b), Ok((1, 2)));
// `try_join!` is variadic, so you can pass any number of futures
let c = async { Ok::<i32, i32>(3) };
let d = async { Ok::<i32, i32>(4) };
let e = async { Ok::<i32, i32>(5) };
assert_eq!(try_join!(c, d, e), Ok((3, 4, 5)));
If one of the futures resolves to an error, try_join!
will return
that error:
use futures::try_join;
let a = async { Ok::<i32, i32>(1) };
let b = async { Err::<u64, i32>(2) };
assert_eq!(try_join!(a, b), Err(2));