How to Write Async Functions That Return Results

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())
}