T
TanStack16mo ago
fascinating-indigo

Difference between a query function returning undefined and a query function throwing an error

I had expected after v4 that returning undefined has the same behavior as throwing an error since undefined won't get added to the cache but in practice it seems like returning an error results in the query function being called far more often. What exactly is happening here?
7 Replies
fascinating-indigo
fascinating-indigoOP16mo ago
ah throwing an error must trigger retries but would e.g. undefined trigger the retry on mount? so undefined results in an error status and doesn't update the cache but isn't treated like a thrown error in terms of retries
national-gold
national-gold16mo ago
Make sure you don't return undefined in the queryFn
national-gold
national-gold16mo ago
Also, the latest docs repeat this for the last bullet of queryFn: https://tanstack.com/query/latest/docs/framework/react/reference/useQuery
Must return a promise that will either resolve data or throw an error. The data cannot be undefined.
fascinating-indigo
fascinating-indigoOP16mo ago
sure, you're not supposed to but you can and nothing particularly bad happens for the purposes of a large migration it might be sane to let old functions keep returning undefined
sunny-green
sunny-green16mo ago
We cannot store undefined and keep the query in success state. I think we warn in dev mode and go to error state. Not sure why we don't retry but it's really an invariant Ah we would need to move the check to the retryer. But retries don't help when you return undefined 🤷‍♂️
fascinating-indigo
fascinating-indigoOP16mo ago
right I think it's fine for undefined to be an error state

Did you find this page helpful?