#await #wait #🦀 #loops #awaiter

wait_not_await

Simple awaiter implementation

3 unstable releases

0.2.1 Mar 28, 2022
0.2.0 Mar 26, 2022
0.1.0 Mar 21, 2022

#12 in #🦀


Used in udpsec

MIT license

8KB
115 lines

🦀 wait_not_await

Simple awaiter implementation in Rust

Examples

Await as a variable

use std::time::Duration;
use wait_not_await::Await;

let mut awaiter = Await::new(move || {
    std::thread::sleep(Duration::from_secs(3));

    "Hello, Wolrd!".to_string()
});

if let Some(result) = awaiter.wait(None) {
    println!("Result: {}", result);
}

Await with functions

use std::time::Duration;
use wait_not_await::Await;

fn async_hello_world() -> Await<String> {
    Await::new(move || {
        std::thread::sleep(Duration::from_secs(2));

        "Hello, World!".to_string()
    })
}

println!("{}", async_hello_world().wait(None).unwrap());

Await result handling

use std::time::Duration;
use wait_not_await::Await;

let awaiter = Await::new(move || {
    std::thread::sleep(Duration::from_secs(3));

    "Hello, Wolrd!".to_string()
});

awaiter.then(move |result| {
    println!("Task result: {}", result);
});

Await loop with result

use std::time::Duration;
use wait_not_await::Await;

fn async_hello_world() -> Await<String> {
    Await::new(move || {
        std::thread::sleep(Duration::from_secs(2));

        "Hello, World!".to_string()
    })
}

let mut awaiter = async_hello_world();
let mut i = 1;

while let None = awaiter.result() {
    println!("Waiting for result: {}", i);

    i += 1;
}

println!("{}", awaiter.result().unwrap());

Author: Nikita Podvirnyy

Licensed under MIT

No runtime deps