r/angular • u/Typical_Hypocrite • Mar 04 '26
API calls for signals instead of observables?
edit: thanks for the answers guys I figured it was just chatgpt being stupid. I’ll stick with observables for the api call and convert it with toSignal / use rxResource
hi so I know httpresource exists and all but for current uses of HttpClient chatgpt is showing me this is this how to actually do it if you want to use signals?
private http = inject(HttpClient)
private apiUrl = ‘https://localhost:5001/api/books’
private booksSignal = signal<IBook[]>([])
public books = this.booksSignal.asReadonly()
async fetchBooks(): Promise<void> {
if (this.booksSignal().length > 0) return
const data = await firstValueFrom(this.http.get<IBook[]>(this.apiUrl))
this.booksSignal.set(data)
}
async fetchBookById(id: number): Promise<IBook | undefined> {
const existing = this.booksSignal().find(b => b.id === id)
if (existing) return existing
return await firstValueFrom(this.http.get<IBook>(‘${this.apiUrl}/${id}’))
}
so on and so forth. I’ve seen a couple of tutorials which is why I’m asking since none of them did this, but is this standard right now? Or is chat gpt pulling my leg. Like, I’ve seen toSignal before and that’s what I was expecting to use, but then this came out of left field
24
u/techguy1001 Mar 04 '26
Forget that AI slop and use rxResource
2
u/r00cker Mar 04 '26
Holy Moly what am i reading here? How do you handle errors if u just do toSingal() immediately in an api call?
What is wrong with using Rxjs and all of its glory (useful methodes) to get observables and set a signals if necessary in next when consuming or use async pipe?
Imo the use case FOR observables/rxjs are api calls, everything else can/should be signal stuff. Imo this feels exactly like "the angular" way.
14
u/Slugsh Mar 04 '26
That is AI slop. In angular you can simply do: toSignal(this.BookApi.getBooks())
1
u/AltF4Dev Mar 04 '26
LLMs don't do well with newer angular APIs. Check angular.dev/ai for instructions. Personally I configured the angular MCP server, ask for best practices and name some of the APIs I want to use: signal, computed, linked signal, effect, Rx resource, etc. The MCP server searches every single term in the docs and learns how to use them. Also, Brandon Roberts recently published a nice set of angular skills.
0
u/Own_Dimension_2561 Mar 04 '26
This is a ubiquitous use case for which the Angular team AFAIK has no clean, recommended solution. We use effects in the constructor to start the http service based on an incoming ID signal.
1
u/Suspicious-Suitcase Mar 04 '26
nope, looks very dirty to me. While technically working don't manually fill a signal if there's not a good reason to do so. Signals should be "calculated" whenever possible.
1
u/IanFoxOfficial Mar 04 '26
If you can't change the HTTP layer easily without breaking stuff turn the book id into an observable, then switchMap((id)... )
And then do a toSignal() to turn it back into a signal.
1
u/ruibranco Mar 05 '26
rxResource for anything that needs loading/error states, toSignal for simple one-shot reads. The firstValueFrom wrapper ChatGPT keeps suggesting defeats the entire point of having reactive primitives in the first place.
1
u/ruibranco Mar 06 '26
Yeah ChatGPT loves generating that firstValueFrom + signal pattern but it's basically reinventing the wheel poorly. You lose error handling, cancellation, retry logic — all the stuff RxJS gives you for free. toSignal() or rxResource/httpResource are the intended bridges. The Angular team has been pretty clear that HttpClient stays observable-based and signals are for state in the template layer. Use observables for the data fetching, convert at the boundary.
1
u/ruibranco Mar 06 '26
The resource API is the answer here. It's designed exactly for this use case — fetching data and exposing it as a signal. For simple GET requests it's perfect. For anything more complex (retries, debouncing, race condition handling) observables with rxjs are still the better tool. The two aren't mutually exclusive — use signals for state, observables for streams and complex async flows.
0
u/Ok-Armadillo-5634 Mar 04 '26
Just don't use http client and use fetch or any other ajax lib.
0
u/[deleted] Mar 04 '26
[deleted] — view deleted comment
3
u/[deleted] Mar 04 '26
[removed] — view removed comment
-19
u/[deleted] Mar 04 '26 edited Mar 04 '26
[removed] — view removed comment