try_join_async_spawn!() { /* proc-macro */ }
Expand description

Use to spawn ::tokio::spawn per each step of each branch. It transposes tuple of Results into Result of tuple or single Result in case of 1 branch.

#![recursion_limit="512"]

extern crate join;
extern crate futures;
extern crate tokio;
extern crate futures_timer;

use join::try_join_async_spawn;
use futures::future::ok;
use futures_timer::Delay;
use std::time::Duration;

#[tokio::main]
async fn main() {
    let product = try_join_async_spawn! {
        ok::<_,()>(2u16) |> |v| Ok::<_,()>(v.unwrap() + 2u16) => |v| async move {
            println!("Hello from parallel world!");
            Delay::new(Duration::from_secs(1)).await.unwrap();
            println!("I'm done.");
            Ok(v)
        },
        ok::<_,()>(3u16) => |v| async move {
            println!("Hello from parallel world again!");
            Delay::new(Duration::from_secs(2)).await.unwrap();
            println!("Me too.");
            Ok(v)
        },
        ok::<_,()>(4u16),
        map => |a, b, c| a * b * c
    }.await.unwrap();

    assert_eq!(product, 48);
}