Repository: tannerlinsley/react-query
File: packages/query-core/src/queryClient.ts (TypeScript)

Code before:
      infer TaggedValue
    >
      ? TaggedValue
      : TQueryFnData,
  >(queryKey: TTaggedQueryKey): TInferredQueryFnData | undefined
  getQueryData(queryKey: QueryKey) {
    const options = this.defaultQueryOptions({ queryKey })
    return this.#queryCache.get(options.queryHash)?.state.data
  }

  ensureQueryData<
    TQueryFnData,
    TError = DefaultError,
    TData = TQueryFnData,
    TQueryKey extends QueryKey = QueryKey,
  >(
    options: EnsureQueryDataOptions<TQueryFnData, TError, TData, TQueryKey>,
  ): Promise<TData> {
    const cachedData = this.getQueryData<TData>(options.queryKey)

    if (cachedData === undefined) return this.fetchQuery(options)
    else {
      const defaultedOptions = this.defaultQueryOptions(options)
      const query = this.#queryCache.build(this, defaultedOptions)

      if (query.isStaleByTime(defaultedOptions.staleTime) && options.revalidateIfStale) {
        void this.prefetchQuery(defaultedOptions)
      }

      return Promise.resolve(cachedData)
    }

  }

  getQueriesData<TQueryFnData = unknown>(
    filters: QueryFilters,
  ): Array<[QueryKey, TQueryFnData | undefined]> {
    return this.getQueryCache()
      .findAll(filters)
      .map(({ queryKey, state }) => {
        const data = state.data as TQueryFnData | undefined
        return [queryKey, data]
      })
  }

  setQueryData<
    TQueryFnData = unknown,
    TTaggedQueryKey extends QueryKey = QueryKey,
    TInferredQueryFnData = TTaggedQueryKey extends DataTag<
      unknown,
      infer TaggedValue

Review comment (suggestion):
let's switch the check for an early bailout of the if statement:

```suggestion
      if (options.revalidateIfStale && query.isStaleByTime(defaultedOptions.staleTime)) {
```

Code after:
      infer TaggedValue
    >
      ? TaggedValue
      : TQueryFnData,
  >(queryKey: TTaggedQueryKey): TInferredQueryFnData | undefined
  getQueryData(queryKey: QueryKey) {
    const options = this.defaultQueryOptions({ queryKey })
    return this.#queryCache.get(options.queryHash)?.state.data
  }

  ensureQueryData<
    TQueryFnData,
    TError = DefaultError,
    TData = TQueryFnData,
    TQueryKey extends QueryKey = QueryKey,
  >(
    options: EnsureQueryDataOptions<TQueryFnData, TError, TData, TQueryKey>,
  ): Promise<TData> {
    const cachedData = this.getQueryData<TData>(options.queryKey)

    if (cachedData === undefined) return this.fetchQuery(options)
    else {
      const defaultedOptions = this.defaultQueryOptions(options)
      const query = this.#queryCache.build(this, defaultedOptions)

      if (
        options.revalidateIfStale &&
        query.isStaleByTime(defaultedOptions.staleTime)
      ) {
        void this.prefetchQuery(defaultedOptions)
      }

      return Promise.resolve(cachedData)
    }
  }

  getQueriesData<TQueryFnData = unknown>(
    filters: QueryFilters,
  ): Array<[QueryKey, TQueryFnData | undefined]> {
    return this.getQueryCache()
      .findAll(filters)
      .map(({ queryKey, state }) => {
        const data = state.data as TQueryFnData | undefined
        return [queryKey, data]
      })
  }

  setQueryData<
    TQueryFnData = unknown,
    TTaggedQueryKey extends QueryKey = QueryKey,
    TInferredQueryFnData = TTaggedQueryKey extends DataTag<