MCPcopy Create free account
hub / github.com/TanStack/query / suspense

Function suspense

packages/vue-query/src/useBaseQuery.ts:225–272  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

223 state.refetch = refetch
224
225 const suspense = () => {
226 return new Promise<QueryObserverResult<TData, TError>>(
227 (resolve, reject) => {
228 let stopWatch = () => {
229 // noop
230 }
231 const run = () => {
232 // Apply the latest options before resolving `enabled`, so a query callback sees the current query.
233 // fix #6133
234 observer.setOptions(defaultedOptions.value)
235 const { enabled } = defaultedOptions.value
236 if (
237 (typeof enabled === 'function'
238 ? enabled(observer.getCurrentQuery())
239 : enabled) !== false
240 ) {
241 const optimisticResult = observer.getOptimisticResult(
242 defaultedOptions.value,
243 )
244 if (optimisticResult.isStale) {
245 stopWatch()
246 observer
247 .fetchOptimistic(defaultedOptions.value)
248 .then(resolve, (error: TError) => {
249 if (
250 shouldThrowError(defaultedOptions.value.throwOnError, [
251 error,
252 observer.getCurrentQuery(),
253 ])
254 ) {
255 reject(error)
256 } else {
257 resolve(observer.getCurrentResult())
258 }
259 })
260 } else {
261 stopWatch()
262 resolve(optimisticResult)
263 }
264 }
265 }
266
267 run()
268
269 stopWatch = watch(defaultedOptions, run)
270 },
271 )
272 }
273
274 // Handle error boundary
275 watch(

Callers

nothing calls this directly

Calls 1

runFunction · 0.70

Tested by

no test coverage detected