Struct promise::Promise [] [src]

pub struct Promise {
    pub value: Arc<Mutex<Option<String>>>,
    pub state: Arc<Mutex<Option<State>>>,
    pub handlers: Arc<Mutex<Option<Vec<Handler>>>>,
    pub thread: JoinHandle<()>,
}

Fields

Methods

impl Promise
[src]

The Promise::await() method is used to wait for a Promise fulfilled.

The Promise::resolve(value) method returns a Promise object that is resolved with the given value.

Examples

use promise::Promise;
let mut promise = Promise::resolve(Some("resolve result".to_string()));
promise.then(|value| {
    println!("value: {:?}", value);
    None
}, |x| x);

The Promise::reject(reason) method returns a Promise object that is rejected with the given reason.

Examples

use promise::Promise;
let mut promise = Promise::reject(Some("reject result".to_string()));
promise.catch(|reason| {
    println!("reason: {:?}", reason);
    None
});

The Promise::all(vec![p1, p2, p3]) method returns a single Promise that resolves when all of the promises in the vector argument have resolved, or rejects with the reason of the first promise that rejects. Resoved results joined with delimeter, by default semicolon;

Examples

use promise::Promise;
let promise1 = Promise::resolve(Some("resolve 1 result".to_string()));
let promise2 = Promise::resolve(Some("reject 2 result".to_string()));
let promise3 = Promise::resolve(Some("resolve 3 result".to_string()));

let promise4 = Promise::new(|resolve, reject| {
    std::thread::sleep(std::time::Duration::from_millis(10));
    resolve(Some("resolve 3 result".to_string()));
});

let mut promise_all = Promise::all(vec![promise1, promise2, promise3, promise4]);

promise_all.then(|value| {
    println!("promise_all: {:?}", value);
    None
}, |_| None );