Skip to content
Source
Sign inSign up
TanStack/query

`experimental_prefetchInRender`: promise never resolves when `select` returns `undefined` #9865

ClosedOliverJAsh(GitHub profile, opens in a new tab) opened this issue 3 comments

Describe the bug

When select returns undefined, the promise never resolves.

  const { promise } = useQuery({
    queryKey: ['projects'],
    queryFn: fetchProjects,
    // This may return `undefined`.
    select: (xs) => xs[100],
  });
  // This will never resolve if `select` returned `undefined`.
  const data = React.use(promise);

Your minimal, reproducible example

https://stackblitz.com/edit/tanstack-query-hockrnrg?file=src%2Findex.tsx&preset=node

Steps to reproduce

  1. Go to the reduced test case.
  2. Observe the promise never resolves.

Expected behavior

The promise resolves with undefined, maybe? 🤔

I appreciate undefined is not valid JSON…

How often does this bug happen?

None

Screenshots or Videos

No response

Platform

N/A

Tanstack Query adapter

None

TanStack Query version

5.90.7

TypeScript version

No response

Additional context

Potentially related: https://github.com/TanStack/query/issues/9844

  1. TkDodocommented

    undefined is not allowed as a cache value, and it’s also not allowed as a return value of select. We could document this better or add a runtime warning in development. PRs welcome, but this is expected behavior.

  2. TkDodo closed this

  3. TkDodo added labelsworks as designed

  4. What are your thoughts on enhancing the types to disallow returning undefined?

  5. TkDodocommented

    We tried that for the queryFn and it's harder than it seems. we'd want to avoid overloads but if you want to give it a try, please do 🙏

Sign in to join this conversation.