Struct asynchronous::Promise [] [src]

pub struct Promise<T, E> {
    // some fields omitted
}

Stores a result of previous execution tasks. T : Type of value returned E : Type of error returned

Methods

impl<T, E> Promise<T, E> where T: Send + 'static, E: Send + 'static
[src]

fn new<F>(f: F) -> Promise<T, E> where F: Send + 'static + FnOnce() -> Result<T, E>

Execute a task inmediatly and returns its Promise.

let promise = asynchronous::Promise::new(|| {
   // Do something  
   if true { Ok("value a")} else { Err("Error description") }
});

fn sync(self) -> Result<T, E>

Syncronizes the execution with the caller, and returns its value.

use asynchronous::Promise;
 
let promise_a = Promise::<_,&str>::new(|| {
   // Do something  
   Ok("value a")
});
let promise_b = Promise::<_,&str>::new(|| {    
   // Do something 
   Ok("value b")
});
// Do something
assert_eq!(promise_a.sync(), Ok("value a"));
assert_eq!(promise_b.sync(), Ok("value b"));

fn all(vector: Vec<Promise<T, E>>) -> Promise<Vec<T>, Vec<Result<T, E>>>

Creates a new promise with the result of all other Promises in the vector.

use asynchronous::Promise;
 
let mut vec_promises = Vec::new();
for i in 0..5 { vec_promises.push(Promise::<_,&str>::new(move || Ok(i) )) }
let res = Promise::all(vec_promises).sync().unwrap();
assert_eq!(res, vec![0,1,2,3,4]);

fn then<TT, EE, FT, FE>(self, ft: FT, fe: FE) -> Promise<TT, EE> where TT: Send + 'static, EE: Send + 'static, FT: Send + 'static + FnOnce(T) -> Result<TT, EE>, FE: Send + 'static + FnOnce(E) -> Result<TT, EE>

Executes only one of the two functions received depending on the result of the previous promise (Ok or Err). Returns a new Promise

let r = asynchronous::Promise::new(|| {
   if false { Ok(1.23) } else { Err("Final error")}
}).then(|res| {
   unreachable!();
   assert_eq!(res, 1.23);
   Ok(34)
}, |err| {
   assert_eq!(err, "Final error");
   if true { Ok(35) } else { Err(44u64)}
}).sync();
assert_eq!(r, Ok(35));

fn chain<TT, EE, F>(self, f: F) -> Promise<TT, EE> where TT: Send + 'static, EE: Send + 'static, F: Send + 'static + FnOnce(Result<T, E>) -> Result<TT, EE>

The same functionality as then, but wraps the result in only one function

let r = asynchronous::Promise::new(|| {
   if false { Ok(1.23) } else { Err("Final error")}
}).chain(|res| {
   // Do something that executes always even on error
   assert!(res.is_err());
   res
}).then(|res| {
   unreachable!();
   assert_eq!(res, 1.23);
   Ok(34)
}, |err| {
   assert_eq!(err, "Final error");
   if true { Ok(35) } else { Err(44u64)}
}).sync();
assert_eq!(r, Ok(35));

fn success<TT, F>(self, f: F) -> Promise<TT, E> where F: Send + 'static + FnOnce(T) -> Result<TT, E>, TT: Send + 'static

Executes a new task if the result of the previous promise is Ok. It may return a new type in a correct result (Ok), but it must return the same type of error of its previous promise.

asynchronous::Promise::new(|| {
   Ok(1.23)
}).success(|res| {
   assert_eq!(res, 1.23);
   Ok(34)
}).success(|res| {
   assert_eq!(res, 34);
   if true { Ok(res) } else { Err("Final error")}
}).sync();

fn fail<F>(self, f: F) -> Promise<T, E> where F: Send + 'static + FnOnce(E) -> Result<T, E>

Executes a new task if the result of the previous promise is Err. It may return a new type in a correct result (Ok), but it must return the same type of error of its previous promise.

asynchronous::Promise::new(|| {
   Err(32)
}).success(|res| {
   unreachable!();
   Ok(res)
}).fail(|err| {
   assert_eq!(err, 32);
   Ok("Value Ok")
}).sync();

fn finally<F>(self, f: F) where F: Send + 'static + FnOnce(Result<T, E>)

Executes one function with the result of the previous promise. It doesn't return anything and it's completly asynchronous.

asynchronous::Promise::new(|| {
   std::thread::sleep_ms(100);
   if true { Ok(32) } else { Err("Error txt") }
}).finally(|res| { 
   assert_eq!(res.unwrap(), 32);
});

let a = 2 + 3;  // This line is executed before the above Promise
 

fn finally_sync<F>(self, f: F) where F: Send + 'static + FnOnce(Result<T, E>)

Executes one function with the result of the previous promise. It doesn't return anything, but it's synchronized with the caller

use asynchronous::Promise;
 
Promise::new(|| {
   std::thread::sleep_ms(100);
   if true { Ok(32) } else { Err("Error txt") }
}).finally_sync(|res| { 
   assert_eq!(res.unwrap(), 32);
});    

let a = 2 + 3;  // This line is executed after the above Promise