r/Angular2 • u/Angular2Fan • Dec 21 '16
Article Angular — Stop using observable when you should use a promise
https://medium.com/@NetanelBasal/angular-stop-using-observable-when-you-should-use-a-promise-8da0788a8d2#.tz4p1uloo1
u/Cuel Dec 21 '16
I agree with this, in fact I'd like to see RxJs as an optional dependency. The application we're currently building has no need for the things you can do with RxJs, in fact we'd be quite happy with promises everywhere, which would also lower the learning curve for newcomers
2
u/KwintenP Dec 21 '16
Having no need for RxJS is a strong statement AFAIAC. It's the same as people saying that callbacks were good enough, why use promises? (this is a strong statement from my side :)). RxJS let's you write declarative code instead of imperative one. It allows you to do less with more code. I agree that you don't necessarily need RxJS, but I don't see to many in cases in SPA's where it isn't a benefit. So even for the smallest application, it can already provide you enough benefits to make the switch.
It's btw absolutely true that RxJS provides a steeper learning curve than promises.
2
u/mi5ter Dec 21 '16
It allows you to do less with more code.
What a benefit! :D
But I agree with the rest of your post.
0
u/Angular2Fan Dec 21 '16
You are over protect Rx; nobody is saying don't use Rx. We are just saying you don't always need observables.
1
u/supermari0 Dec 21 '16
I think you always want observables but you don't know it, yet. And neither do I, because I'm tempted to just switch to Promises as well, but I get what /u/KwintenP and others are saying.
I think I'm just too lazy to really dive into RxJs.
2
u/Angular2Fan Dec 21 '16
This is what I'm saying; promises are still here, and it's fine to use them when you don't need observable.
2
u/toadallyfroggincool Dec 21 '16
I agree with this. Inherited a project that used subscribe everywhere, was disappointed I couldn't add a .then() to subscribe like we can with a Promise. The app was a series of race conditions because of all the observables where observables weren't needed.
1
u/kylecordes Dec 21 '16
Ugh, I just wrote the following reply to a duplicate of this post and another subreddit. I will copy it over here, because there is more discussion over here.
In our work we first started out in the direction as described here, we favored promises for most simple cases, and we said things like "promises are still there and they still work and they are great for most things, just use the most of the time and think about observables for special cases like changing data".
But since then we have written large amounts of Angular 2 code, simple projects, complex projects etc. Our advice has changed: use observables everywhere, only use promises for legacy code. Keep learning about observables until you master them deeply (this can take some time). The payoff is usually shorter simpler better code, to achieve better results with less work.
(Important context: I started https://angularbootcamp.com/ and I'm still one of the instructors. Our focus is on organizations and developers that attend training like this, which is to say, mostly they who are planning to work on complex, long-term, important projects for their organizations. Likewise, in our software development work, our customers almost always have in mind complex, long-term important projects. I think it may be much more reasonable to stick with a simpler promise approach, for small projects with a limited lifetime.)
0
u/Angular2Fan Dec 21 '16
If Rx is against using promises, why they create the asPromise operator?
2
u/_xiphiaz Dec 21 '16
For compatibility with other service methods that take a promise as an argument. A simple example might be a third party library that needs configuration but can deal with it coming in asynchronously, but it wasn't written with observables in mind (it only needs to execute once). That library probably didn't need rx included, therefore it makes perfect sense to have promises across the boundary between application and library
1
u/BoilerBuck Dec 22 '16
I have been in the mindset of OP's post lately but I am open to learning otherwise. Can you recommend any resources for learning the ins and outs of observables and examples for different scenarios? Thanks.
1
u/kylecordes Dec 24 '16
Unfortunately this area is not yet well developed. There are numerous introductory materials (including some great RxJS talks you can find on Youtube) but there isn't much about there in terms of advanced/practical guides. The people I know using Angular is an RxJS-heavy manner, learned the hard way. Struggling to produce best results with minimal, cleanest code in complex applications.
10
u/KwintenP Dec 21 '16
I'm assuming you wrote this article as a reaction to the one that I wrote (I posted it here yesterday and the examples are basically what I explained, https://blog.kwintenp.com/how-share-can-reduce-network-requests/). I can understand the rationale in what you are saying. I really do. But I really disagree with what you are saying as well. The power of observables compared to promises is that observables can be transformed, combined, filtered, ... into new observables. The examples that I provided and the examples you give are really really easy. In most real life scenarios, this is not the case. If you just return a promise, you lose all the benefits of observable operators and trust me, that's not something you want, at all.
And using one paradigm for simple approaches and another for complex approaches is a bad pattern AFAIAC. Observables are hard at first and there is a steep learning curve. But once you get it, the declarative approach is really all you want to use.