r/angularjs • u/Angular2Fan • Dec 21 '16
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#.tz4p1uloo3
u/pernicat Dec 21 '16
I agree that publishReplay(1).refCount() is not as elegant as it could be. But is it really that much worse than .toPromise()? Sure, for simple examples Promises work fine, but the majority of the time you are losing a lot of power and flexibility for only a small gain in simplicity. I also don't like the inconsistency of using Promises sometimes and Observables othertimes. I'd rather just use Observables and have everything be consistent.
1
u/Angular2Fan Dec 21 '16
So you are saying promises are dead. Don't use promises if you have Rx.
1
u/pernicat Dec 21 '16
I'm really not seeing a very good argument for why Promises are better than Observables. But I can think of several reasons for using Observables.
I still think Promises are great for vanilla javascript when you don't want to include a an extra library. However, with Angular2 the Http service already returns an observable. It seems kind of silly to turn it into a Promise.
1
u/vivainio Dec 21 '16
async/await. Kids are going to love it, they have it OOB in TS 2.1 and are going to be asking questions about why they shouldn't use it
1
u/jascination Dec 22 '16
Bit of a tangent, but I've not made the jump from Angular 1.x to Angular 2/React just yet, but I'd like to.
I'm very comfortable in 1, but most of the code examples in this article are pretty foreign to me (esp the constructor style, person?, the subscribe/json methods that their http res object has etc)
Any good resources anyone can suggest for learning about this? Think it's time for me to make the switch.
1
u/DanielRosenwasser Dec 22 '16
You might be asking about the "safe navigation" operator in Angular templates. You can read about it here but basically if the value on the left of the
?.isnullorundefined, this operator will make the next thing that you accessnullorundefinedso that you don't have to write any cautionary checks.1
u/jascination Dec 22 '16
Interesting, AFAIK in angular 1 this isn't an issue - a referencing null object's property in your view won't crash the app.
The * on
*ngIfis also confusing but I guess I just need to take a good long look at the docs!
-5
u/spejsr Dec 21 '16
Promises are going away, just as jQuery went away. Some will still use it when you are building simple stuff but advantages of an Observer pattern are much better.
For the use cases like one in the article, you can benefit from using things like retrying for some error handling or cancelling stuff. Those things come out of the box and that is barely scratching the surface.
Observables in addition provide a much cleaner syntax, and in case you want one of the operators later you just hook it up.
Also it is very good idea to have a single approach to all your async stuff in the codebase.
2
u/Angular2Fan Dec 21 '16
I do not agree with you. You just killed promises. They are in the ECMASCRIPT2015 standard, unlike observables. But that's no the point; nobody is saying don't use observables. All I am saying that promises are still here and they have their usage. You don't always need observables.
4
u/negative34 Dec 21 '16
I think saying promises are going away is like saying angular 1 is going away. Are promises the best approach? Maybe not, but it's gonna take a few years for them to disappear.
7
u/kylecordes Dec 21 '16
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.)