asynchronous

Asynchronous promises in rust using threads

|Crate|Travis| |:------:|:-------:| ||Build Status|

| API Documentation |

Overview

This library provides an usefull way to invoke functions (clousures) in a Promise Style using separated threads. A Promise is a Struct that represents the return value or the error that the funcion produces.

It also allows the execution of tasks in Parallel or Series in deferred, joining the result in a Promise.

It includes methods to manage Event Loops, where there are tasks that "emit" events in background, and they are collected by a promise.

Project github page

This project is based on the Q Promise library for Node JS and Async.js

License

Dual-licensed to be compatible with the Rust project.

Licensed under the Apache License, Version 2.0 http://www.apache.org/licenses/LICENSE-2.0 or the MIT license http://opensource.org/licenses/MIT, at your option. This file may not be copied, modified, or distributed except according to those terms.

Examples

This is a simple setup for a promise based execution:

```rust use asynchronous::Promise;

Promise::new(|| { // Do something
let ret = 10.0 / 3.0; if ret > 0.0 { Ok(ret) } else { Err("Incorrect Value") } }).success(|res| { // res has type f64 // Do something if the previous result is correct asserteq!(res, 10.0 / 3.0); let resint = res as u32 * 2; Ok(resint) }).finallysync(|res| { // res has type u32 // Catch a correct result assert_eq!(res, 6u32); }, |error| { // Catch an incorrect result unreachable!(); }); ```

Deferred execution:

```rust use asynchronous::{Promise,Deferred,ControlFlow};

let da = Deferred::<&str, &str>::new(|| { Ok("a") }); let pb = Promise::<&str, &str>::new(|| { Ok("b") }); // Executed right now let d1 = Deferred::::new(|| { Ok(1u32) }); let d2 = Deferred::::new(|| { Err("Mock Error") }); let d3 = Deferred::::new(|| { Ok(3u32) }); let d4 = Deferred::::new(|| { Ok(4u32) }); let d5 = Deferred::::new(|| { Ok(5u32) });

let promise = Deferred::vectopromise(vec![d1,d2,d3], ControlFlow::Parallel); // Only d1, d2 and d3 are being executed at this time.

asserteq!("ab", da.topromise().success(|resa| { pb.success(move |resb| { Ok(resa.tostring() + res_b) }).sync() }).sync().unwrap());

promise.success(|res| { // Catch the result. In this case, tasks d4 and d5 never will be executed Ok(res) }).fail(|error| { // Catch the error and execute another Promise asserteq!(error, vec![Ok(1u32), Err("Mock Error"), Ok(3u32)]);
Deferred::vec
topromise(vec![d4,d5], ControlFlow::Series).sync() }).finallysync(|res| { // res : Vec // Do something here
assert_eq!(res, vec![4u32, 5u32]); }, |error| { // error : Vec> // Check Errors });

```

Simple event loop:

```rust use asynchronous::EventLoop;

let el = EventLoop::new().finishinms(100); el.emit("Event1"); el.emit("Event2"); // Do something here el.topromise().finallysync(|res| { // res: Vec assert_eq!(res, vec!["Event1", "Event2"]); }, |error| { // Check Errors }); ```