T
TanStack2y ago
wise-white

I want to load a query's data inside an async function

Hi all, I have a feature in my app where the user can print out a report, now inside the async function to print the report I want to access a query's data but I'm not sure how to do that, I have created a helper function which works but I'm not sure if this is a good way to do it
const loadQuery = <T, E>(
queryFactory: () => CreateQueryResult<T, E>,
injector: Injector
) => {
return runInInjectionContext(injector, () => {
const query = queryFactory();

return lastValueFrom(
toObservable(query.data as Signal<T>).pipe(filter(Boolean), take(1))
);
});
};
const loadQuery = <T, E>(
queryFactory: () => CreateQueryResult<T, E>,
injector: Injector
) => {
return runInInjectionContext(injector, () => {
const query = queryFactory();

return lastValueFrom(
toObservable(query.data as Signal<T>).pipe(filter(Boolean), take(1))
);
});
};
1 Reply
genetic-orange
genetic-orange2y ago
It's possible to get data directly from the queryClient. https://tanstack.com/query/latest/docs/react/reference/QueryClient The query client in both React Query and Angular Query are 100% identical so you can refer to the React Query docs on how to use it. In Angular you'd get the queryClient using injectQueryClient.
QueryClient | TanStack Query Docs
QueryClient The QueryClient can be used to interact with a cache:

Did you find this page helpful?