pub struct TimeoutFuture { /* private fields */ }
Expand description

A scheduled timeout as a Future.

See TimeoutFuture::new for scheduling new timeouts.

Once scheduled, if you change your mind and don’t want the timeout to fire, you can drop the future.

A timeout future will never resolve to Err. Its only failure mode is when the timeout is so long that it is effectively infinite and never fires.

Example

use gloo_timers::future::TimeoutFuture;
use futures_util::future::{select, Either};
use wasm_bindgen_futures::spawn_local;

spawn_local(async {
    match select(TimeoutFuture::new(1_000), TimeoutFuture::new(2_000)).await {
        Either::Left((val, b)) => {
            // Drop the `2_000` ms timeout to cancel its timeout.
            drop(b);
        }
        Either::Right((a, val)) => {
            panic!("the `1_000` ms timeout should have won this race");
        }
    }
});

Implementations

Create a new timeout future.

Remember that futures do nothing unless polled or spawned, so either pass this future to wasm_bindgen_futures::spawn_local or use it inside another future.

Example
use gloo_timers::future::TimeoutFuture;
use wasm_bindgen_futures::spawn_local;

spawn_local(async {
    TimeoutFuture::new(1_000).await;
    // Do stuff after one second...
});

Trait Implementations

Formats the value using the given formatter. Read more

The type of value produced on completion.

Attempt to resolve the future to a final value, registering the current task for wakeup if the value is not yet available. Read more

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Performs the conversion.

Performs the conversion.

🔬 This is a nightly-only experimental API. (into_future)

The output that the future will produce on completion.

🔬 This is a nightly-only experimental API. (into_future)

Which kind of future are we turning this into?

🔬 This is a nightly-only experimental API. (into_future)

Creates a future from a value.

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.