1010 - loading
1111 - promises
1212 - ssr
13- version : ' 1.0'
13+ version : " 1.0"
1414description : >-
1515 Transform promises into reactive signals with createAsync. Handle async data
1616 with Suspense and automatic loading states.
@@ -34,21 +34,21 @@ import { createAsync } from "@solidjs/router";
3434
3535``` tsx
3636function createAsync<T >(
37- fn : (prev : T ) => Promise <T >,
38- options : {
39- name? : string ;
40- initialValue: T ;
41- deferStream? : boolean ;
42- }
37+ fn : (prev : T ) => Promise <T >,
38+ options : {
39+ name? : string ;
40+ initialValue: T ;
41+ deferStream? : boolean ;
42+ }
4343): AccessorWithLatest <T >;
4444
4545function createAsync<T >(
46- fn : (prev : T | undefined ) => Promise <T >,
47- options ? : {
48- name? : string ;
49- initialValue? : T ;
50- deferStream? : boolean ;
51- }
46+ fn : (prev : T | undefined ) => Promise <T >,
47+ options ? : {
48+ name? : string ;
49+ initialValue? : T ;
50+ deferStream? : boolean ;
51+ }
5252): AccessorWithLatest <T | undefined >;
5353```
5454
@@ -105,36 +105,70 @@ While the fetcher is executing for the first time, unless an `initialValue` is s
105105``` tsx
106106import { createAsync , query } from " @solidjs/router" ;
107107
108- const getUserQuery = query (async (id : string ) => {
109- // ... Fetches the user from the server.
110- }, " user " );
108+ const getCurrentUser = query (async () => {
109+ // ... Fetches the current authenticated user from the server.
110+ }, " currentUser " );
111111
112112function UserProfile() {
113- const user = createAsync (() => getUserQuery ());
113+ const user = createAsync (() => getCurrentUser ());
114114
115- return <p >{ user ()?.name } </p >;
115+ return <div >{ user ()?.name } </div >;
116116}
117117```
118118
119- ### Handling loading and errors
119+ ### With parameter
120120
121121``` tsx
122- import { Suspense , ErrorBoundary , For } from " solid-js" ;
123122import { createAsync , query } from " @solidjs/router" ;
124123
125- const getUserQuery = query (async (id : string ) => {
126- // ... Fetches the user from the server.
127- }, " user " );
124+ const getInvoiceQuery = query (async (invoiceId : string ) => {
125+ // ... Fetches the invoice details from the server.
126+ }, " invoice " );
128127
129- function UserProfile() {
130- const user = createAsync (() => getUserQuery ());
128+ function InvoiceDetails(props : { invoiceId: string }) {
129+ const invoice = createAsync (() => getInvoiceQuery (props .invoiceId ));
130+
131+ return (
132+ <div >
133+ <h2 >Invoice #{ invoice ()?.number } </h2 >
134+ <p >Total: ${ invoice ()?.total } </p >
135+ </div >
136+ );
137+ }
138+ ```
139+
140+ ### With error handling and pending state
141+
142+ ``` tsx
143+ import { createAsync , query } from " @solidjs/router" ;
144+ import { Suspense , ErrorBoundary , For } from " solid-js" ;
145+
146+ const getAllRecipesQuery = query (async () => {
147+ // ... Fetches the recipes from the server and throws an error if an issue occurred.
148+ }, " recipes" );
149+
150+ function Recipes() {
151+ const recipes = createAsync (() => getAllRecipesQuery ());
131152
132153 return (
133- <ErrorBoundary fallback = { <p >Could not fetch user data.</p >} >
134- <Suspense fallback = { <p >Loading user...</p >} >
135- <p >{ user ()! .name } </p >
154+ <ErrorBoundary fallback = { <p >Couldn't fetch any recipes!</p >} >
155+ <Suspense fallback = { <p >Fetching recipes...</p >} >
156+ <For each = { recipes ()} >
157+ { (recipe ) => (
158+ <div >
159+ <h3 >{ recipe .name } </h3 >
160+ <p >Cook time: { recipe .time } </p >
161+ </div >
162+ )}
163+ </For >
136164 </Suspense >
137165 </ErrorBoundary >
138166 );
139167}
140168```
169+
170+ ## Related
171+
172+ - [ ` query ` ] ( /solid-router/reference/data-apis/query )
173+ - [ ` <Suspense> ` ] ( /reference/components/suspense )
174+ - [ ` <ErrorBoundary> ` ] ( /reference/components/error-boundary )
0 commit comments