Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

why should we use subscribe() over map() in Angular?

I am trying to take advantage of observables in angular2 and got confused on why should i use map() over subscribe(). Suppose i am getting values from a webApi, like this

  this.http.get('http://172.17.40.41:8089/api/Master/GetAllCountry') 

Now using subscribe(success, error, complete) I can get all the values on the success callback and I can return the values on the complete callback. If I can do all theses functionalities then what is the need of map()? Does it give any advantage?

In short, why one should write like this:

this.http.get('http://172.17.40.41:8089/api/Master/GetAllCountry')     .map(r=>{})     .subscribe(value => {     }, error => error, () => { }); 

when they can simply write this without the map function:

this.http.get('http://172.17.40.41:8089/api/Master/GetAllCountry')     .subscribe(value => {             }, error => error, () => {            }); 
like image 330
Lijin Durairaj Avatar asked Feb 20 '17 11:02

Lijin Durairaj


People also ask

Why we are using subscribe in Angular?

Subscribe() is a method in Angular that connects the observer to observable events. Whenever any change is made in these observable, a code is executed and observes the results or changes using the subscribe method. Subscribe() is a method from the rxjs library, used internally by Angular.

What is difference between subscribe and Observable in Angular?

Observables are not executed until a consumer subscribes. The subscribe() executes the defined behavior once, and it can be called again. Each subscription has its own computation. Resubscription causes recomputation of values.

What is the use of Subscription in RxJS?

A Subscription is an object that represents a disposable resource, usually the execution of an Observable. A Subscription has one important method, unsubscribe , that takes no argument and just disposes the resource held by the subscription. In previous versions of RxJS, Subscription was called "Disposable".

Which is better promise or Observable?

The biggest difference is that Promises won't change their value once they have been fulfilled. They can only emit (reject, resolve) a single value. On the other hand, observables can emit multiple results. The subscriber will be receiving results until the observer is completed or unsubscribed from.


2 Answers

If you want to return an Observable some other code can subscribe to, but you still want to manipulate the data events in the current method, use map.

The actual user of the observable needs to subscribe(), because without subscribe() the observable won't be executed at all. (forEach() or toArray() and probably others work as well to execute the observable instead of subscribe())

subscribe() returns a Subscription that can not be subscribed to, but it can be used to cancel the subscription.

map() returns an Observable which can be subscribed to.

like image 104
Günter Zöchbauer Avatar answered Oct 19 '22 08:10

Günter Zöchbauer


Think map as a middleware which transforms the response.

this.http.get('http://172.17.40.41:8089/api/Master/GetAllCountry') .map(r=>r.json())  .subscribe(result => {               // here result would have json object that was parsed by map handler...             },failurCallback,completeCallback) 

subscribe is used to invoke the observable, please read a good doc on cold-vs-hot-observables

like image 38
A.T. Avatar answered Oct 19 '22 09:10

A.T.