Skip to content

Commit 89e3c07

Browse files
fix(vue-query): prevent uncaught exception in suspense() (#5825)
Co-authored-by: ssendev <[email protected]>
1 parent 9259866 commit 89e3c07

File tree

1 file changed

+23
-19
lines changed

1 file changed

+23
-19
lines changed

packages/vue-query/src/useBaseQuery.ts

Lines changed: 23 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -117,29 +117,33 @@ export function useBaseQuery<
117117
})
118118

119119
const suspense = () => {
120-
return new Promise<QueryObserverResult<TData, TError>>((resolve) => {
121-
let stopWatch = () => {
122-
//noop
123-
}
124-
const run = () => {
125-
if (defaultedOptions.value.enabled !== false) {
126-
const optimisticResult = observer.getOptimisticResult(
127-
defaultedOptions.value,
128-
)
129-
if (optimisticResult.isStale) {
130-
stopWatch()
131-
resolve(observer.fetchOptimistic(defaultedOptions.value))
132-
} else {
133-
stopWatch()
134-
resolve(optimisticResult)
120+
return new Promise<QueryObserverResult<TData, TError>>(
121+
(resolve, reject) => {
122+
let stopWatch = () => {
123+
//noop
124+
}
125+
const run = () => {
126+
if (defaultedOptions.value.enabled !== false) {
127+
const optimisticResult = observer.getOptimisticResult(
128+
defaultedOptions.value,
129+
)
130+
if (optimisticResult.isStale) {
131+
stopWatch()
132+
observer
133+
.fetchOptimistic(defaultedOptions.value)
134+
.then(resolve, reject)
135+
} else {
136+
stopWatch()
137+
resolve(optimisticResult)
138+
}
135139
}
136140
}
137-
}
138141

139-
run()
142+
run()
140143

141-
stopWatch = watch(defaultedOptions, run, { deep: true })
142-
})
144+
stopWatch = watch(defaultedOptions, run, { deep: true })
145+
},
146+
)
143147
}
144148

145149
return {

0 commit comments

Comments
 (0)