Struct gloo_timers::future::TimeoutFuture
source · 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§
source§impl TimeoutFuture
impl TimeoutFuture
sourcepub fn new(millis: u32) -> TimeoutFuture ⓘ
pub fn new(millis: u32) -> TimeoutFuture ⓘ
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§
source§impl Debug for TimeoutFuture
impl Debug for TimeoutFuture
source§impl Future for TimeoutFuture
impl Future for TimeoutFuture
Auto Trait Implementations§
impl !RefUnwindSafe for TimeoutFuture
impl !Send for TimeoutFuture
impl !Sync for TimeoutFuture
impl Unpin for TimeoutFuture
impl !UnwindSafe for TimeoutFuture
Blanket Implementations§
source§impl<F> IntoFuture for Fwhere
F: Future,
impl<F> IntoFuture for Fwhere
F: Future,
§type IntoFuture = F
type IntoFuture = F
Which kind of future are we turning this into?
source§fn into_future(self) -> <F as IntoFuture>::IntoFuture
fn into_future(self) -> <F as IntoFuture>::IntoFuture
Creates a future from a value. Read more