Skip to content

Commit 18113c8

Browse files
Lenz Webermarkerikson
authored andcommitted
return promise from query result & hook refetch
1 parent ed3d162 commit 18113c8

File tree

2 files changed

+10
-5
lines changed

2 files changed

+10
-5
lines changed

packages/toolkit/src/query/core/buildInitiate.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ export type QueryActionCreatorResult<
5656
abort(): void
5757
unwrap(): Promise<ResultTypeFrom<D>>
5858
unsubscribe(): void
59-
refetch(): void
59+
refetch(): QueryActionCreatorResult<D>
6060
updateSubscriptionOptions(options: SubscriptionOptions): void
6161
queryCacheKey: string
6262
}
@@ -311,11 +311,10 @@ Features like automatic cache collection, automatic refetching etc. will not be
311311

312312
return result.data
313313
},
314-
refetch() {
314+
refetch: () =>
315315
dispatch(
316316
queryAction(arg, { subscribe: false, forceRefetch: true })
317-
)
318-
},
317+
),
319318
unsubscribe() {
320319
if (subscribe)
321320
dispatch(

packages/toolkit/src/query/react/buildHooks.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -771,7 +771,13 @@ export function buildHooks<Definitions extends EndpointDefinitions>({
771771
/**
772772
* A method to manually refetch data for the query
773773
*/
774-
refetch: () => void promiseRef.current?.refetch(),
774+
refetch: () => {
775+
if (!promiseRef.current)
776+
throw new Error(
777+
'Cannot refetch a query that has not been started yet.'
778+
)
779+
return promiseRef.current?.refetch()
780+
},
775781
}),
776782
[]
777783
)

0 commit comments

Comments
 (0)