Function futures_lite::future::race

source ·
pub fn race<T, F1, F2>(future1: F1, future2: F2) -> Race<F1, F2> 
where F1: Future<Output = T>, F2: Future<Output = T>,
Expand description

Returns the result of the future that completes first, with no preference if both are ready.

Each time Race is polled, the two inner futures are polled in random order. Therefore, no future takes precedence over the other if both can complete at the same time.

If you have preference for one of the futures, use the or() function or the FutureExt::or() method.

§Examples

use futures_lite::future::{self, pending, ready};

assert_eq!(future::race(ready(1), pending()).await, 1);
assert_eq!(future::race(pending(), ready(2)).await, 2);

// One of the two futures is randomly chosen as the winner.
let res = future::race(ready(1), ready(2)).await;