Skip to content

Commit 8ce7f15

Browse files
authored
fix(vue-query): prevent uncaught exception in suspense() (#5619)
1 parent a745e13 commit 8ce7f15

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
@@ -98,29 +98,33 @@ export function useBaseQuery<
9898
})
9999

100100
const suspense = () => {
101-
return new Promise<QueryObserverResult<TData, TError>>((resolve) => {
102-
let stopWatch = () => {
103-
//noop
104-
}
105-
const run = () => {
106-
if (defaultedOptions.value.enabled !== false) {
107-
const optimisticResult = observer.getOptimisticResult(
108-
defaultedOptions.value,
109-
)
110-
if (optimisticResult.isStale) {
111-
stopWatch()
112-
resolve(observer.fetchOptimistic(defaultedOptions.value))
113-
} else {
114-
stopWatch()
115-
resolve(optimisticResult)
101+
return new Promise<QueryObserverResult<TData, TError>>(
102+
(resolve, reject) => {
103+
let stopWatch = () => {
104+
//noop
105+
}
106+
const run = () => {
107+
if (defaultedOptions.value.enabled !== false) {
108+
const optimisticResult = observer.getOptimisticResult(
109+
defaultedOptions.value,
110+
)
111+
if (optimisticResult.isStale) {
112+
stopWatch()
113+
observer
114+
.fetchOptimistic(defaultedOptions.value)
115+
.then(resolve, reject)
116+
} else {
117+
stopWatch()
118+
resolve(optimisticResult)
119+
}
116120
}
117121
}
118-
}
119122

120-
run()
123+
run()
121124

122-
stopWatch = watch(defaultedOptions, run, { deep: true })
123-
})
125+
stopWatch = watch(defaultedOptions, run, { deep: true })
126+
},
127+
)
124128
}
125129

126130
return {

0 commit comments

Comments
 (0)