Function bevy::tasks::futures_lite::stream::race

pub fn race<T, S1, S2>(stream1: S1, stream2: S2) -> Race<S1, S2>
where S1: Stream<Item = T>, S2: Stream<Item = T>,
Available on crate features std and race only.
Expand description

Merges two streams, with no preference for either stream when both are ready.


use futures_lite::stream::{self, once, pending, StreamExt};

assert_eq!(stream::race(once(1), pending()).next().await, Some(1));
assert_eq!(stream::race(pending(), once(2)).next().await, Some(2));

// One of the two stream is randomly chosen as the winner.
let res = stream::race(once(1), once(2)).next().await;