Function leptos::Await

source ·
pub fn Await<T, Fut, FF, VF, V>(
    props: AwaitProps<T, Fut, FF, VF, V>
) -> impl IntoView
where Fut: Future<Output = T> + 'static, FF: Fn() -> Fut + 'static, V: IntoView, VF: Fn(&T) -> V + 'static, T: Serializable + 'static,
Expand description

Allows you to inline the data loading for an async block or server function directly into your view. This is the equivalent of combining a create_resource that only loads once (i.e., with a source signal || ()) with a Suspense with no fallback.

Adding let:{variable name} to the props makes the data available in the children that variable name, when resolved.

async fn fetch_monkeys(monkey: i32) -> i32 {
    // do some expensive work
    3
}

view! {
    <Await
        future=|| fetch_monkeys(3)
        let:data
    >
        <p>{*data} " little monkeys, jumping on the bed."</p>
    </Await>
}

§Required Props

  • future: [FF]
    • A function that returns the Future that will the component will .await before rendering.
  • children: [VF]
    • A function that takes a reference to the resolved data from the future renders a view.

      §Syntax

      This can be passed in the view children of the <Await/> by using the let: syntax to specify the name for the data variable.

      view! {
          <Await
              future=|| fetch_monkeys(3)
              let:data
          >
              <p>{*data} " little monkeys, jumping on the bed."</p>
          </Await>
      }

      is the same as

      view! {
         <Await
             future=|| fetch_monkeys(3)
             children=|data| view! {
               <p>{*data} " little monkeys, jumping on the bed."</p>
             }
         />
      }

§Optional Props

  • blocking: bool
    • If true, the component will use create_blocking_resource, preventing the HTML stream from returning anything before future has resolved.
  • local: bool
    • If true, the component will use create_local_resource, this will always run on the local system and therefore its result type does not need to be Serializable.