MCPcopy Create free account
hub / github.com/buraksenyurt/rust-training / run

Function run

S23_asynchronous_programming/src/api_sample.rs:3–24  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

1use tokio::task;
2
3pub async fn run() {
4 let task_a = task::spawn(fetch_data_async(
5 "https://jsonplaceholder.typicode.com/posts/1",
6 ));
7 let task_b = task::spawn(fetch_data_async(
8 "https://jsonplaceholder.typicode.com/posts/2",
9 ));
10 let task_c = task::spawn(fetch_data_async(
11 "https://jsonplaceholder.typicode.com/posts/3",
12 ));
13
14 let (res_a, res_b, res_c) = tokio::join!(task_a, task_b, task_c);
15
16 match (res_a, res_b, res_c) {
17 (Ok(a), Ok(b), Ok(c)) => {
18 println!("{:?}", a);
19 println!("{:?}", b);
20 println!("{:?}", c);
21 }
22 _ => println!("Failed to fetch data"),
23 }
24}
25
26async fn fetch_data_async(url: &str) -> Result<String, reqwest::Error> {
27 let response = reqwest::get(url).await?;

Callers

nothing calls this directly

Calls 1

fetch_data_asyncFunction · 0.85

Tested by

no test coverage detected