diff --git a/src/routes/solid-router/reference/data-apis/create-async.mdx b/src/routes/solid-router/reference/data-apis/create-async.mdx index bfbdac5497..11d1f6b809 100644 --- a/src/routes/solid-router/reference/data-apis/create-async.mdx +++ b/src/routes/solid-router/reference/data-apis/create-async.mdx @@ -10,7 +10,7 @@ tags: - loading - promises - ssr -version: '1.0' +version: "1.0" description: >- Transform promises into reactive signals with createAsync. Handle async data with Suspense and automatic loading states. @@ -34,21 +34,21 @@ import { createAsync } from "@solidjs/router"; ```tsx function createAsync( - fn: (prev: T) => Promise, - options: { - name?: string; - initialValue: T; - deferStream?: boolean; - } + fn: (prev: T) => Promise, + options: { + name?: string; + initialValue: T; + deferStream?: boolean; + } ): AccessorWithLatest; function createAsync( - fn: (prev: T | undefined) => Promise, - options?: { - name?: string; - initialValue?: T; - deferStream?: boolean; - } + fn: (prev: T | undefined) => Promise, + options?: { + name?: string; + initialValue?: T; + deferStream?: boolean; + } ): AccessorWithLatest; ``` @@ -105,36 +105,70 @@ While the fetcher is executing for the first time, unless an `initialValue` is s ```tsx import { createAsync, query } from "@solidjs/router"; -const getUserQuery = query(async (id: string) => { - // ... Fetches the user from the server. -}, "user"); +const getCurrentUser = query(async () => { + // ... Fetches the current authenticated user from the server. +}, "currentUser"); function UserProfile() { - const user = createAsync(() => getUserQuery()); + const user = createAsync(() => getCurrentUser()); - return

{user()?.name}

; + return
{user()?.name}
; } ``` -### Handling loading and errors +### With parameter ```tsx -import { Suspense, ErrorBoundary, For } from "solid-js"; import { createAsync, query } from "@solidjs/router"; -const getUserQuery = query(async (id: string) => { - // ... Fetches the user from the server. -}, "user"); +const getInvoiceQuery = query(async (invoiceId: string) => { + // ... Fetches the invoice details from the server. +}, "invoice"); -function UserProfile() { - const user = createAsync(() => getUserQuery()); +function InvoiceDetails(props: { invoiceId: string }) { + const invoice = createAsync(() => getInvoiceQuery(props.invoiceId)); + + return ( +
+

Invoice #{invoice()?.number}

+

Total: ${invoice()?.total}

+
+ ); +} +``` + +### With error handling and pending state + +```tsx +import { createAsync, query } from "@solidjs/router"; +import { Suspense, ErrorBoundary, For } from "solid-js"; + +const getAllRecipesQuery = query(async () => { + // ... Fetches the recipes from the server and throws an error if an issue occurred. +}, "recipes"); + +function Recipes() { + const recipes = createAsync(() => getAllRecipesQuery()); return ( - Could not fetch user data.

}> - Loading user...

}> -

{user()!.name}

+ Couldn't fetch any recipes!

}> + Fetching recipes...

}> + + {(recipe) => ( +
+

{recipe.name}

+

Cook time: {recipe.time}

+
+ )} +
); } ``` + +## Related + +- [`query`](/solid-router/reference/data-apis/query) +- [``](/reference/components/suspense) +- [``](/reference/components/error-boundary)