Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add async/await support to useQuery #127

Closed
YoSev opened this issue Dec 6, 2024 · 4 comments
Closed

feat: add async/await support to useQuery #127

YoSev opened this issue Dec 6, 2024 · 4 comments

Comments

@YoSev
Copy link

YoSev commented Dec 6, 2024

Feature request: add async/await support to useQuery to enable fetching synchronously.

I know this defeats the purpose of state management (thus the whole project) a bit but it would be nice to use the caching layer while fetching data synchronously.

@posva
Copy link
Owner

posva commented Dec 6, 2024

It doesn't defeat the purpose of the library 😄 . I think it makes sense if you want to use it with suspense. You can then just await the refresh() call:

const { data, refresh } = useQuery({ /* ... */ })
await refresh()

I haven't tried it thought, but it should work. In practice I would actually go for data loaders instead of Suspense

@NoeReverse
Copy link

Another way to utilize the caching layer is to manipulate the Query Cache. You can access the Query Cache using the useQueryCache composable. You can check for the presence of data by the key using queryCache.getQueryData(key), and if the data is not in the cache, run a query and add it there using queryCache.setQueryData(key, data). You can create a separate composable for this purpose.

However, it would be very good to have this feature in the library.

Copy link
Owner

posva commented Dec 7, 2024

Or you can just call refresh 😄

@YoSev
Copy link
Author

YoSev commented Dec 7, 2024

Thank you guys. I think this library could indeed need sync support.
For now, i went for awaiting the refresh:

const res = useQuery({
    key: k.value,
    staleTime: ttl !== undefined ? ttl : 0,
    enabled: false,
    query: () => method(params)
  })
await res.refresh()

@posva posva closed this as completed Dec 7, 2024
@github-project-automation github-project-automation bot moved this from 🆕 Triaging to Done in Pinia Colada Roadmap Dec 7, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Archived in project
Development

No branches or pull requests

3 participants