-
Notifications
You must be signed in to change notification settings - Fork 655
Open
Labels
Description
I am trying to write something to wait some time and check the async function is finished so I was using FutureExt::now_or_never
but it is giving me unexpected output. The following is the example program I was using to test.
Cargo.toml
[package]
name = "futures-test"
version = "0.1.0"
edition = "2021"
[dependencies]
futures = "0.3.28"
futures-timer = "3.0.2"
main.rs
use std::time::Duration;
use futures::{FutureExt, executor};
use futures_timer::Delay;
async fn wait_for_signal() -> i32 {
Delay::new(Duration::from_millis(5)).await;
return 42
}
fn main() {
let timeout = Duration::from_secs(5);
let signal_future = wait_for_signal();
executor::block_on(Delay::new(timeout));
let signal = signal_future.now_or_never();
println!("Signal: {:?}", signal);
// if signal.is_none() {
// println!("Timed out");
// } else {
// println!("Got data");
// }
}
The output I am getting is None even though the delay in wait_for_signal
should have already been done.
Compiling futures-test v0.1.0 (/root/futures-test)
Finished dev [unoptimized + debuginfo] target(s) in 0.20s
Running `target/debug/futures-test`
Signal: None
But if I comment out Delay::new(Duration::from_millis(5)).await;
in wait_for_signal
, it is giving me what I expect.
Compiling futures-test v0.1.0 (/root/futures-test)
Finished dev [unoptimized + debuginfo] target(s) in 0.21s
Running `target/debug/futures-test`
Signal: Some(42)