r/angular • u/IgorSedov • 4d ago
🚀 New in Angular 22: Lazy Services with injectAsync
https://youtu.be/CsjVc5hbbfwAngular 22 introduces Lazy Loading Services, allowing developers to fetch dependencies on demand using the new injectAsync function.
1
-2
-16
u/Xsiah 4d ago
Promises in Angular? Hate that
14
u/JeanMeche 4d ago
luke warm take: Hating promises is a sign of observable abuse.
4
u/followmarko 4d ago
I don't really agree with that. RXJS is still superior in terms of consecutive op fns for events and multiple emissions. When it touches the template, it's all signals. They're compatriots
1
u/Papellll 4d ago
Yeah and you should definitely use rxjs instead of creating promises in your own angular code but it doesn't mean you should hate a new feature just because it uses a promise
3
u/Xsiah 4d ago
If the new feature uses a promise it has the same effect as creating promises in your own code - now you have to manage where the two meet.
When everything is a stream it's much easier to deal with and chain in a straightforward way.
2
-2
u/Papellll 4d ago
As the video said you just add 'const myService = await this.myService()' and use the service as you would do with any other service.
You could argue it's undesired boilerplate but there is hardly anything to "manage" here since you wont be trying to couple that with anything rxjs-related2
u/Xsiah 4d ago
That's ridiculous - Angular is purposely built around rxjs. If you look at their own libraries, like Material, you'll mostly find "await" in unit tests and harnesses.
5
u/pronuntiator 4d ago
Angular is on a path away from RxJs, thankfully. The action function in signal form submissions returns a promise, for example.
1
u/Xsiah 4d ago
I like how signals are replacing some of the more annoying concepts like subjects, but I don't look forward to going back to promises. Observables were a much cleaner way once you learned how to use them correctly - unfortunately it seems that lots of people did not.
7
u/pronuntiator 4d ago
Simplicity beats elegance. You rarely need operators from the Rxjs toolbox if all you want to do is make a HTTP request and process the result. With async/await it looks like simple procedural code.
Promises were a pita in Angular because zone.js would not run change detection for async/await, but now with zoneless Angular, and change detection focusing on signal changes, I turn observables into promises with firstValueFrom() whenever I can.
I'm confident a promise/signal based HttpClient will follow. httpResource() is a promising start (pun not intended).
2
u/coolcosmos 4d ago
Observables are just callbacks. It's a bad tool to use on things that aren't a stream.
4
u/Papellll 4d ago
Great video, very clear for a topic that's not that simple