map vs subscribe in @ngrx/store

4.5k views Asked by At

I am new to angular2, currently, I am working on a project which using @ngrx/store and subscribe classes. I am confused on map method is not working while subscribe store is working. Please help me in understanding when to use map and subscribe method. The code is below.

this.store.select('rating').subscribe(({ topPicks }) => { // code is working });
this.store.select('rating').map(({ topPicks }) => // not working);
1

There are 1 answers

0
Pradeepb On BEST ANSWER

From what I understand, here this.store.select('rating') is an Observable. Depends on whether the observable is Hot or Cold, (For more information read this blog) observable will start emitting values. In your case, first statement works because you are subscribing to observable. Map is just an operator which you can use it on the values emitted by an observable(its does not work as subscriber).

Second statement does not work as there is no subscriber to the Observable. To use map operator, you can do like below:

this.store.select('rating').map(/* Do whatever you want to with the emitted values here */).subscribe(({ topPicks }) => {});

To know more about operators, you can have a look at documentation.