Which project does this relate to?#
Router
Describe the bug#
I tried to use the defer function with queryClient.fetchQuery. But the <Await> component returned undefined for the resolved data. I have looked at the code of the defer and noticed that when I used the defer function in loader, property status was already added to the promise. Then, the defer function was continuing to be used in the useAwaited hook. This time, the defer function inside the useAwaited hook could not assign property data to the promise because the condition was not met. So the resolved data is undefined. I tried to fix the issue by moving the logic for promise.then out of the condition if (!(promise as any).status). The issue was resolved but I am not sure if it is a legit solution. So, I wrote a customDefer function. It can be found at /utils/customDefer.ts in the codesandbox


Your Example Website or App#
https://codesandbox.io/p/devbox/n2jq68
Steps to Reproduce the Bug or Issue#
- Go to https://codesandbox.io/p/devbox/n2jq68
- Notices the right section has the "Loading todos..." text. After 3-4 seconds, the text shows "resolvedTodos is undefined"
- The logic for the loader function can be found at
/routes/index.tsx
Expected behavior#
I expected the useAwaited hook can return the resolved data properly when passing a deferred promise fetched by queryClient
Screenshots or Videos#
No response
Platform#
- Browser: Chrome, Microsoft Edge
- Version: 131.0
Additional context#
No response
This is a mistake in the usage.
The usage of defer here doesn't make sense, since Query is already an asynchronous state management library and therefore mixing it with this primitive is bound to result in an error. When using Query (or a similar library), you want to use its own access patterns for retrieving the fetched data.
So, in the
loaderyou'll just be kicking off the query and awaiting thegetPostsQueryOptions().Then in the Route component, you'd be accessing the data using the correct hooks from your async state library of choice. You won't be using
Route.useLoaderData().I'll be sure to add a note in the deferred data loading guide, so callout the fact that when you are using an external library for this kind of caching, you'll only be using the loader to kick-off the fetch.