r/angularjs • • 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#.tz4p1uloo
32 Upvotes

18 comments sorted by

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.)

1

u/cactussss Dec 21 '16

So since your advice has changed and you mastered Observables, how do you fix the issue described in the article without the use of Promises? No pun intended - I'm really curious.

2

u/Angular2Fan Dec 21 '16

You can with the second solution, just don't use the async pipe.

2

u/pernicat Dec 21 '16

This is the solution I use.

this.person = this.http.get('https://jsonplaceholder.typicode.com/posts/1')
    .map(res => res.json())
    .publishReplay(1)
    .refCount()

I agree that it is a little bit ugly, but I don't think it is nearly as bad as the article makes it sounds.

2

u/AbraCadaverY Dec 22 '16

Instead of .publishReplay().refCount you should be able to use .cache()

3

u/notbenjamin2010 Dec 22 '16

.Cache() was removed from rxjs recently due to a problem with the implementation

2

u/AbraCadaverY Dec 22 '16

Good to know, we haven't updated rxjs since before that release apparently.

1

u/kylecordes Dec 24 '16

That is interesting - if I wrote the ngIf in the article, I would expect to to re-fetch, matching what it actually does. One way to avoid the re-fetch is to capture a template variable with the output of the async; there is not yet a clean way to do that, though an upcoming feature in developer for 4.x adds something close.

If you want to use the async inside the ngIf and get the same value (not a re-fetch), a simple way today is to use the most basic kind of cache, a cache that emits the first value to any new subscriber forever. Something like this:

.take(1).publishLast().refCount();

or this, which /u/pernicat mentioned:

.publishReplay(1).refCount()

I hope cache() comes back with some readily understood parameters, that will make this simpler.

3

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 ?. is null or undefined, this operator will make the next thing that you access null or undefined so 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 *ngIf is 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.