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

Method fetch

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

Source from the content-addressed store, hash-verified

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

Callers 12

setupDevtoolsFunction · 0.45
handleRefetchFunction · 0.45
QueryDetailsFunction · 0.45
persisterFnFunction · 0.45
refetchQueriesMethod · 0.45
queryMethod · 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