Skip to content

Commit cc1ecc6

Browse files
author
Lenz Weber
committed
return promise from query result & hook refetch
1 parent 478ffcf commit cc1ecc6

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
@@ -55,7 +55,7 @@ export type QueryActionCreatorResult<
5555
abort(): void
5656
unwrap(): Promise<ResultTypeFrom<D>>
5757
unsubscribe(): void
58-
refetch(): void
58+
refetch(): QueryActionCreatorResult<D>
5959
updateSubscriptionOptions(options: SubscriptionOptions): void
6060
queryCacheKey: string
6161
}
@@ -300,11 +300,10 @@ Features like automatic cache collection, automatic refetching etc. will not be
300300

301301
return result.data
302302
},
303-
refetch() {
303+
refetch: () =>
304304
dispatch(
305305
queryAction(arg, { subscribe: false, forceRefetch: true })
306-
)
307-
},
306+
),
308307
unsubscribe() {
309308
if (subscribe)
310309
dispatch(

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -695,7 +695,13 @@ export function buildHooks<Definitions extends EndpointDefinitions>({
695695
/**
696696
* A method to manually refetch data for the query
697697
*/
698-
refetch: () => void promiseRef.current?.refetch(),
698+
refetch: () => {
699+
if (!promiseRef.current)
700+
throw new Error(
701+
'Cannot refetch a query that has not been started yet.'
702+
)
703+
return promiseRef.current?.refetch()
704+
},
699705
}),
700706
[]
701707
)

0 commit comments

Comments
 (0)