MCPcopy Create free account
hub / github.com/SvelteStack/svelte-query / fetch

Method fetch

src/queryCore/core/query.ts:375–513  ·  view source on GitHub ↗
(
    options?: QueryOptions<TQueryFnData, TError, TData, TQueryKey>,
    fetchOptions?: FetchOptions
  )

Source from the content-addressed store, hash-verified

373 }
374
375 fetch(
376 options?: QueryOptions<TQueryFnData, TError, TData, TQueryKey>,
377 fetchOptions?: FetchOptions
378 ): Promise<TData> {
379 if (this.state.isFetching) {
380 if (this.state.dataUpdatedAt && fetchOptions?.cancelRefetch) {
381 // Silently cancel current fetch if the user wants to cancel refetches
382 this.cancel({ silent: true })
383 } else if (this.promise) {
384 // make sure that retries that were potentially cancelled due to unmounts can continue
385 this.retryer?.continueRetry()
386 // Return current promise if we are already fetching
387 return this.promise
388 }
389 }
390
391 // Update config if passed, otherwise the config from the last execution is used
392 if (options) {
393 this.setOptions(options)
394 }
395
396 // Use the options from the first observer with a query function if no function is found.
397 // This can happen when the query is hydrated or created with setQueryData.
398 if (!this.options.queryFn) {
399 const observer = this.observers.find(x => x.options.queryFn)
400 if (observer) {
401 this.setOptions(observer.options)
402 }
403 }
404
405 const queryKey = ensureQueryKeyArray(this.queryKey)
406 const abortController = getAbortController()
407
408 // Create query function context
409 const queryFnContext: QueryFunctionContext<TQueryKey> = {
410 queryKey,
411 pageParam: undefined,
412 meta: this.meta,
413 }
414
415 Object.defineProperty(queryFnContext, 'signal', {
416 enumerable: true,
417 get: () => {
418 if (abortController) {
419 this.abortSignalConsumed = true
420 return abortController.signal
421 }
422 return undefined
423 },
424 })
425
426 // Create fetch function
427 const fetchFn = () => {
428 if (!this.options.queryFn) {
429 return Promise.reject('Missing queryFn')
430 }
431 this.abortSignalConsumed = false
432 return this.options.queryFn(queryFnContext)

Callers 2

refetchQueriesMethod · 0.45
fetchQueryMethod · 0.45

Calls 10

cancelMethod · 0.95
setOptionsMethod · 0.95
dispatchMethod · 0.95
setDataMethod · 0.95
optionalRemoveMethod · 0.95
ensureQueryKeyArrayFunction · 0.90
getAbortControllerFunction · 0.90
isCancelledErrorFunction · 0.90
getLoggerFunction · 0.90
findMethod · 0.45

Tested by

no test coverage detected