pub struct Promise<T: 'static = ()>(_);
Expand description
Represents an ongoing operation which produces a result.
Implementations§
source§impl<T: Send + Sync + 'static> Promise<T>
impl<T: Send + Sync + 'static> Promise<T>
sourcepub fn new() -> (Self, impl FnOnce(T) + Send)
pub fn new() -> (Self, impl FnOnce(T) + Send)
Creates a new lightweight promise and an associated resolve function.
Example
let (promise, resolve) = Promise::<i32>::new();
Task::spawn(|| {
Task::delay(Duration::from_secs(1));
resolve(10);
})
.unwrap();
println!(
"n = {}",
select! {
n = promise.done() => n,
}
);
sourcepub fn done(&self) -> impl Selectable<&T> + '_
pub fn done(&self) -> impl Selectable<&T> + '_
A Selectable
event which occurs when the promise is resolved.
sourcepub fn spawn(f: impl FnOnce() -> T + Send + 'static) -> Self
pub fn spawn(f: impl FnOnce() -> T + Send + 'static) -> Self
Spawns a task to run the given function and returns a Promise
that
resolves with the result when it returns. Panics on failure; see
Promise::try_spawn()
.
sourcepub fn try_spawn(f: impl FnOnce() -> T + Send + 'static) -> Result<Self, Error>
pub fn try_spawn(f: impl FnOnce() -> T + Send + 'static) -> Result<Self, Error>
Spawns a task to run the given function and returns a Promise
that
resolves with the result when it returns.
sourcepub fn then<U: Send + Sync + 'static>(
&self,
f: impl FnOnce(&T) -> U + Send + 'static
) -> Promise<U>
pub fn then<U: Send + Sync + 'static>(
&self,
f: impl FnOnce(&T) -> U + Send + 'static
) -> Promise<U>
Spawns a new promise which, upon the completion of self
, runs f
.