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, () => { });
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.
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.
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".
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.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With