MCPcopy Index your code
hub / github.com/TanStack/query / fetch

Method fetch

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

Source from the content-addressed store, hash-verified

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

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 7

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

Tested by

no test coverage detected