Open foohyfooh opened 1 year ago
See this playground.
You might think that the 5 seconds wait started when signal_future
was created - in the line
let signal_future = wait_for_signal();
But async code are lazy and do nothing unless polled. The body of wait_for_signal
wasn't executed until when the signal_future
was polled in the line
let signal = signal_future.now_or_never();
Playground Code
Thanks to your playground I noticed something interesting between the handling of sleep
vs Delay
. now_or_never
waits for the future when using sleep
for the additional time and returns the value; but doesn't do that with Delay
and returns the None.
Since playground only gives the output after the whole program is run or halted, you may be required to run the playground code locally to properly see the difference.
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
main.rs
The output I am getting is None even though the delay in
wait_for_signal
should have already been done.But if I comment out
Delay::new(Duration::from_millis(5)).await;
inwait_for_signal
, it is giving me what I expect.