pub trait Join {
type Output;
type Future: Future<Output = Self::Output>;
// Required method
fn join(self) -> Self::Future;
}
Expand description
Wait for all futures to complete.
Awaits multiple futures simultaneously, returning the output of the futures in the same container type they were created once all complete.
Required Associated Types§
Required Methods§
sourcefn join(self) -> Self::Future
fn join(self) -> Self::Future
Waits for multiple futures to complete.
Awaits multiple futures simultaneously, returning the output of the futures in the same container type they we’re created once all complete.
§Examples
Awaiting multiple futures of the same type can be done using either a vector or an array.
use futures_concurrency::prelude::*;
// all futures passed here are of the same type
let fut1 = core::future::ready(1);
let fut2 = core::future::ready(2);
let fut3 = core::future::ready(3);
let outputs = [fut1, fut2, fut3].join().await;
assert_eq!(outputs, [1, 2, 3]);
In practice however, it’s common to want to await multiple futures of
different types. For example if you have two different async {}
blocks,
you want to .await
. To do that, you can call .join
on tuples of futures.
use futures_concurrency::prelude::*;
async fn some_async_fn() -> usize { 3 }
// the futures passed here are of different types
let fut1 = core::future::ready(1);
let fut2 = async { 2 };
let fut3 = some_async_fn();
// ^ NOTE: no `.await` here!
let outputs = (fut1, fut2, fut3).join().await;
assert_eq!(outputs, (1, 2, 3));
This function returns a new future which polls all futures concurrently.