Function async_ui_web::join
source · pub fn join<F>(f: F) -> <F as Join>::Futurewhere
F: Join,Expand description
Wait for multiple futures to complete.
Join takes in many “subfutures” and return a single Future. When awaited, the returned Future will drive all the subfutures to completion and return all their results.
Subfutures may be passed in as either
- a tuple of up to 12 Futures (signature:
(F1, F2, ...) -> (F1::Output, F2::Output, ...)) - an array of Futures (signature:
[F; N] -> [F::Output; N]) - a Vec of Futures (signature:
Vec<F> -> Vec<F::Output>)
async fn do_something(input: i32) -> i32 {
// ...make a network request of something...
input * 2
}
// Join 2-tuple of Futures
let (res_1, res_2) = join((
do_something(21),
do_something(100)
)).await;
assert_eq!(res_1, 42);
assert_eq!(res_2, 200);
// Join array of Futures
let results: [i32; 20] = join(
core::array::from_fn(|idx| do_something(idx as i32))
).await;
assert_eq!(
&results,
&*(0..20).map(|x| x * 2).collect::<Vec<_>>()
);
// Join vector of Futures
let results: Vec<i32> = join(
(0..100).map(|i| do_something(i as i32)).collect::<Vec<_>>()
).await;
assert_eq!(
results,
(0..100).map(|x| x * 2).collect::<Vec<_>>()
);