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

Method fetch

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

Source from the content-addressed store, hash-verified

398 }
399
400 async fetch(
401 options?: QueryOptions<TQueryFnData, TError, TData, TQueryKey>,
402 fetchOptions?: FetchOptions<TQueryFnData>,
403 ): Promise<TData> {
404 if (
405 this.state.fetchStatus !== 'idle' &&
406 // If the promise in the retryer is already rejected, we have to definitely
407 // re-start the fetch; there is a chance that the query is still in a
408 // pending state when that happens
409 this.#retryer?.status() !== 'rejected'
410 ) {
411 if (this.state.data !== undefined && fetchOptions?.cancelRefetch) {
412 // Silently cancel current fetch if the user wants to cancel refetch
413 this.cancel({ silent: true })
414 } else if (this.#retryer) {
415 // make sure that retries that were potentially cancelled due to unmounts can continue
416 this.#retryer.continueRetry()
417 // Return current promise if we are already fetching
418 return this.#retryer.promise
419 }
420 }
421
422 // Update config if passed, otherwise the config from the last execution is used
423 if (options) {
424 this.setOptions(options)
425 }
426
427 // Use the options from the first observer with a query function if no function is found.
428 // This can happen when the query is hydrated or created with setQueryData.
429 if (!this.options.queryFn) {
430 const observer = this.observers.find((x) => x.options.queryFn)
431 if (observer) {
432 this.setOptions(observer.options)
433 }
434 }
435
436 if (process.env.NODE_ENV !== 'production') {
437 if (!Array.isArray(this.options.queryKey)) {
438 console.error(
439 `As of v4, queryKey needs to be an Array. If you are using a string like 'repoData', please change it to an Array, e.g. ['repoData']`,
440 )
441 }
442 }
443
444 const abortController = new AbortController()
445
446 // Adds an enumerable signal property to the object that
447 // which sets abortSignalConsumed to true when the signal
448 // is read.
449 const addSignalProperty = (object: unknown) => {
450 Object.defineProperty(object, 'signal', {
451 enumerable: true,
452 get: () => {
453 this.#abortSignalConsumed = true
454 return abortController.signal
455 },
456 })
457 }

Callers 11

setupDevtoolsFunction · 0.45
handleRefetchFunction · 0.45
QueryDetailsFunction · 0.45
persisterFnFunction · 0.45
refetchQueriesMethod · 0.45
fetchQueryMethod · 0.45
hydrateFunction · 0.45
fetchNextPageMethod · 0.45
fetchPreviousPageMethod · 0.45
query.test.tsxFile · 0.45

Calls 8

cancelMethod · 0.95
setOptionsMethod · 0.95
#dispatchMethod · 0.95
setStateMethod · 0.95
setDataMethod · 0.95
infiniteQueryBehaviorFunction · 0.90
createRetryerFunction · 0.90
findMethod · 0.45

Tested by

no test coverage detected