Skip to content

Commit

Permalink
return promise from query result & hook refetch
Browse files Browse the repository at this point in the history
  • Loading branch information
Lenz Weber committed Apr 7, 2022
1 parent 478ffcf commit cc1ecc6
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 5 deletions.
7 changes: 3 additions & 4 deletions packages/toolkit/src/query/core/buildInitiate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ export type QueryActionCreatorResult<
abort(): void
unwrap(): Promise<ResultTypeFrom<D>>
unsubscribe(): void
refetch(): void
refetch(): QueryActionCreatorResult<D>
updateSubscriptionOptions(options: SubscriptionOptions): void
queryCacheKey: string
}
Expand Down Expand Up @@ -300,11 +300,10 @@ Features like automatic cache collection, automatic refetching etc. will not be

return result.data
},
refetch() {
refetch: () =>
dispatch(
queryAction(arg, { subscribe: false, forceRefetch: true })
)
},
),
unsubscribe() {
if (subscribe)
dispatch(
Expand Down
8 changes: 7 additions & 1 deletion packages/toolkit/src/query/react/buildHooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -695,7 +695,13 @@ export function buildHooks<Definitions extends EndpointDefinitions>({
/**
* A method to manually refetch data for the query
*/
refetch: () => void promiseRef.current?.refetch(),
refetch: () => {
if (!promiseRef.current)
throw new Error(
'Cannot refetch a query that has not been started yet.'
)
return promiseRef.current?.refetch()
},
}),
[]
)
Expand Down

0 comments on commit cc1ecc6

Please sign in to comment.