Define an async function with the async fn keyword and use .await to get results from asynchronous operations.
Write an async function using the async fn keyword and return the result directly from the function body. Use .await on any asynchronous operations inside the function to pause execution until the result is ready.
use trpl::Html;
async fn page_title(url: &str) -> Option<String> {
let response_text = trpl::get(url).await.text().await;
Html::parse(&response_text)
.select_first("title")
.map(|title| title.inner_html())
}
An async function is a special function that can pause its work to wait for slow tasks, like fetching data from the internet, without freezing the rest of your program. It returns a result only after all the waiting is done. Think of it like ordering food at a restaurant: you place the order and wait at the table, but the waiter can still take orders from other people while your food is being cooked.