React Router API Reference
    Preparing search index...

    Function Await

    • Used to render promise values with automatic error handling.

      Note: <Await> expects to be rendered inside a <React.Suspense>

      Pass unstable_promise instead of resolve to opt into React's use() implementation. With unstable_promise, errorElement handles promise rejections, while errors thrown by children bubble to the nearest React error boundary.

      In React Server Components, resolve already has this error-handling behavior: errorElement handles only input rejections, while child render errors propagate to the surrounding error boundary. Both props accept promise-like values without modifying the source promise. Both props treat a falsy errorElement as absent and propagate the rejection. Pass an empty fragment to handle a rejection without rendering any content.

      The unstable_promise prop is experimental and may change in minor or patch releases.

      Type Parameters

      • Resolve

      Parameters

      • props: AwaitProps<Resolve> | AwaitPromiseProps<Resolve>

        Props

        • AwaitProps<Resolve>
          • children: ReactNode | AwaitResolveRenderFunction<Resolve>

            When using a function, the resolved value is provided as the parameter.

            <Await resolve={reviewsPromise}>
            {(resolvedReviews) => <Reviews items={resolvedReviews} />}
            </Await>

            When using React elements, useAsyncValue will provide the resolved value:

            <Await resolve={reviewsPromise}>
            <Reviews />
            </Await>

            function Reviews() {
            const resolvedReviews = useAsyncValue();
            return <div>...</div>;
            }
          • OptionalerrorElement?: ReactNode

            The error element renders instead of the children when the Promise rejects.

            <Await
            errorElement={<div>Oops</div>}
            resolve={reviewsPromise}
            >
            <Reviews />
            </Await>

            To provide a more contextual error, you can use the useAsyncError in a child component

            <Await
            errorElement={<ReviewsError />}
            resolve={reviewsPromise}
            >
            <Reviews />
            </Await>

            function ReviewsError() {
            const error = useAsyncError();
            return <div>Error loading reviews: {error.message}</div>;
            }

            If you do not provide an errorElement, the rejected value will bubble up to the nearest route-level ErrorBoundary and be accessible via the useRouteError hook.

          • resolve: Resolve

            Takes a Promise returned from a loader to be resolved and rendered.

            import { Await, useLoaderData } from "react-router";

            export async function loader() {
            let reviews = getReviews(); // not awaited
            let book = await getBook();
            return {
            book,
            reviews, // this is a promise
            };
            }

            export default function Book() {
            const {
            book,
            reviews, // this is the same promise
            } = useLoaderData();

            return (
            <div>
            <h1>{book.title}</h1>
            <p>{book.description}</p>
            <React.Suspense fallback={<ReviewsSkeleton />}>
            <Await
            // and is the promise we pass to Await
            resolve={reviews}
            >
            <Reviews />
            </Await>
            </React.Suspense>
            </div>
            );
            }
          • Optionalunstable_promise?: undefined

            Use either resolve or unstable_promise, never both.

        • AwaitPromiseProps<Resolve>
          • children

            n/a

          • errorElement

            n/a

          • resolve

            n/a

          • unstable_promise

            A promise (or value) to read using React's use() API in client components. Accepts promise-like values without modifying the source promise. Pass exactly one of unstable_promise or resolve.

      Returns Element

      React element for the rendered awaited value

      import { Await, useLoaderData } from "react-router";

      export async function loader() {
      // not awaited
      const reviews = getReviews();
      // awaited (blocks the transition)
      const book = await fetch("/api/book").then((res) => res.json());
      return { book, reviews };
      }

      function Book() {
      const { book, reviews } = useLoaderData();
      return (
      <div>
      <h1>{book.title}</h1>
      <p>{book.description}</p>
      <React.Suspense fallback={<ReviewsSkeleton />}>
      <Await
      resolve={reviews}
      errorElement={
      <div>Could not load reviews 😬</div>
      }
      children={(resolvedReviews) => (
      <Reviews items={resolvedReviews} />
      )}
      />
      </React.Suspense>
      </div>
      );
      }

      @public

      framework

      data