Skip to content

FutureExt::now_or_never always returning None when using futures_timer::Delay #2745

@foohyfooh

Description

@foohyfooh

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)

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions