T
TanStack•14mo ago
grumpy-cyan

Why do I need to use `lastValueFrom` to convert Observable to Promise in TanStack Angular Query?*

I'm using TanStack Query in an Angular project, and I need to use lastValueFrom to convert an Observable to a Promise in the query function. Here's an example of my code:
import { of, lastValueFrom } from 'rxjs';
...
const query = injectQuery(() => ({
queryKey: ['repoData'],
queryFn: () => lastValueFrom(of([{ ... }])),
}));
import { of, lastValueFrom } from 'rxjs';
...
const query = injectQuery(() => ({
queryKey: ['repoData'],
queryFn: () => lastValueFrom(of([{ ... }])),
}));
My question is, why do I need to explicitly use lastValueFrom to convert the Observable to a Promise? Shouldn't the framework internally check if the result is an Observable and handle the conversion itself? In angular most of the use is Observable rather than Promise. Is there a specific reason for this design choice, or am I missing something in how TanStack Angular Query handles Observables? Any insights or explanations would be greatly appreciated!
2 Replies
genetic-orange
genetic-orange•14mo ago
Query works with promises, not observables. Iirc someone wanted to integrate observable support
genetic-orange
genetic-orange•14mo ago
why would you ask an angular question in #react-query-questions rather than #angular-query-questions 😅 ?

Did you find this page helpful?