macro_rules! first {
($( $arg:expr ),*) => { ... };
}
Expand description
Polls for the first future to complete, and then cancels the remaining ones.
If you care about the return value, use select
. This macro must
be used in an async
context, such as an async fn
or async { }
block.
FuturesUnordered
can be more flexible and efficient than this macro when you have many
Future
s or need to dynamically add and remove them.
ยงExample
The call to block_on
completes after 5 seconds or
when the escape key is pressed, whichever comes first.
In reality, you should use
TimerListener::timeout
for this purpose.
However, first!
can be used for other, more complex cases.
use ndless_async::task::{AsyncListeners, block_on};
use ndless_async::StreamExt;
use ndless::input::Key;
let listeners = AsyncListeners::new();
block_on(&listeners, async { first!(timeout(&listeners), listen_for_esc(&listeners)) });
async fn timeout(listeners: &AsyncListeners) {
listeners.timer().sleep_ms(5000).await;
}
async fn listen_for_esc(listeners: &AsyncListeners) {
let mut keypad = listeners.keypad();
while let Some(event) = keypad.next().await {
if event.key == Key::Esc {
break;
}
}
}