@@ -163,29 +163,56 @@ export function useLoadData<T extends NotUndefined, Deps extends any[]>(
163
163
const initialPromise = useMemo ( ( ) => {
164
164
const correctedArgs = correctOptionalDependencies ( fetchDataArgs ) ;
165
165
if ( ! data && counter < 1 && checkArgsAreLoaded ( correctedArgs ) ) {
166
- return fetchData ( ...( ( correctedArgs . map ( unboxApiResponse ) || [ ] ) as Parameters < typeof fetchData > ) ) ;
166
+ try {
167
+ return {
168
+ res : fetchData ( ...( ( correctedArgs . map ( unboxApiResponse ) || [ ] ) as Parameters < typeof fetchData > ) ) ,
169
+ error : undefined
170
+ } ;
171
+ } catch ( e ) {
172
+ return {
173
+ res : undefined ,
174
+ error : e
175
+ } ;
176
+ }
167
177
} else {
168
- return undefined ;
178
+ return { res : undefined , error : undefined } ;
169
179
}
170
180
} , [ counter ] ) ;
171
181
172
- const nonPromiseResult = initialPromise instanceof Promise ? undefined : initialPromise ;
182
+ const nonPromiseResult = initialPromise . res instanceof Promise ? undefined : initialPromise . res ;
173
183
const initialData = data || nonPromiseResult ;
174
184
185
+ // Initialize our pending data to one of three possible states:
186
+ // 1. If initial data was supplied or if the fetchData function returned a non-Promise value,
187
+ // then our initial state will be already "resolved" (not in-progress and not error, we already have the result)
188
+ // 2. If initial data was not supplied and fetchData returned a Promise, then our initial state is in-progress
189
+ // 3. If initial data was not supplied and fetchData threw a *synchronous* (non-Promise) exception,
190
+ // then our initial state is "rejected" (not in-progress and already has an error value)
191
+ const initialDataResolved =
192
+ initialData &&
193
+ ( {
194
+ isInProgress : false ,
195
+ isError : false ,
196
+ result : initialData ,
197
+ error : undefined
198
+ } as const ) ;
199
+ const initialDataRejected =
200
+ initialPromise . error !== undefined &&
201
+ ( {
202
+ isInProgress : false ,
203
+ isError : true ,
204
+ result : undefined ,
205
+ error : initialPromise . error
206
+ } as const ) ;
207
+ const initialDataPending = {
208
+ isInProgress : true ,
209
+ isError : false ,
210
+ result : undefined ,
211
+ error : undefined
212
+ } as const ;
213
+
175
214
const [ pendingData , setPendingData ] = useState < ApiResponse < T > > (
176
- initialData
177
- ? {
178
- isInProgress : false ,
179
- isError : false ,
180
- result : initialData ,
181
- error : undefined
182
- }
183
- : {
184
- isInProgress : true ,
185
- isError : false ,
186
- result : undefined ,
187
- error : undefined
188
- }
215
+ initialDataResolved || initialDataRejected || initialDataPending
189
216
) ;
190
217
191
218
function retry ( ) {
@@ -224,9 +251,9 @@ export function useLoadData<T extends NotUndefined, Deps extends any[]>(
224
251
const unboxedArgs = correctedArgs . map ( unboxApiResponse ) ;
225
252
226
253
const fetchedData =
227
- initialPromise === undefined
254
+ initialPromise . res === undefined
228
255
? await fetchData ( ...( ( unboxedArgs || [ ] ) as Parameters < typeof fetchData > ) )
229
- : await initialPromise ;
256
+ : await initialPromise . res ;
230
257
231
258
setPendingData ( {
232
259
isInProgress : false ,
0 commit comments