Delay the evaluation of a paramless async function and cache the result. Adapted from mafintosh/thunky
Let's make a simple function that returns a random number 1 second after it is called for the first time
```rust extern crate thunky; extern crate rand; use thunky::*; use rand::Rng;
fn main () {
let run = move |thunk: &Thunky
let thunk = Thunky::new(Box::new(run));
// I miss JavaScript's `setTimeout()`
let thunk_clone = Arc::clone(&thunk);
let when = Instant::now() + Duration::from_millis(1000);
let task = Delay::new(when)
.and_then(move |_| {
let mut rng = rand::thread_rng();
thunk_clone.cache(Ok(rng.gen::<u32>()));
Ok(())
})
.map_err(|e| panic!("delay errored; err={:?}", e));
thunk.run(Box::new(|arg: &Result<u32, &str>| -> () {
println!("{}", arg.unwrap()); // prints random number
}));
tokio::run(task);
thunk.run(Box::new(|arg: &Result<u32, &str>| -> () {
println!("{}", arg.unwrap()); // prints the same random number as above
}));
} ```
If the thunk cache function is called with an Err<E>
, it will not cache the result
```rust
let v = Mutex::new(0);
let run = move |thunk: &Thunky
let thunk = Thunky::new(Box::new(run));
thunk.run(Box::new(|arg: &Result
thunk.run(Box::new(|arg: &Result
sh
cargo add thunky