timeout_demo/
timeout_demo.rs

1use avila_async::{Runtime, timeout, sleep};
2use std::time::Duration;
3
4async fn slow_operation() -> i32 {
5    sleep(Duration::from_secs(5)).await;
6    42
7}
8
9async fn fast_operation() -> i32 {
10    sleep(Duration::from_millis(100)).await;
11    100
12}
13
14fn main() {
15    let rt = Runtime::new();
16
17    rt.block_on(async {
18        // This will timeout
19        match timeout(Duration::from_secs(1), slow_operation()).await {
20            Ok(val) => println!("Slow operation completed: {}", val),
21            Err(_) => println!("Slow operation timed out!"),
22        }
23
24        // This will succeed
25        match timeout(Duration::from_secs(1), fast_operation()).await {
26            Ok(val) => println!("Fast operation completed: {}", val),
27            Err(_) => println!("Fast operation timed out!"),
28        }
29    });
30}