I recently started exploring observables in angular2 and found myself puzzled over the decision to use map()
instead of subscribe()
.
Let's say I am fetching data from a webApi, like so:
this.http.get('http://172.17.40.41:8089/api/Master/GetAllCountry')
With
subscribe(success, error, complete)
, I can retrieve all values in the success callback and return the values in the complete callback. So, why would I need to use map()
? Does it offer any advantages?
In essence, why should one write code like this:
this.http.get('http://172.17.40.41:8089/api/Master/GetAllCountry')
.map(r=>{})
.subscribe(value => {
}, error => error, () => {
});
when they could simply write this without using the map function:
this.http.get('http://172.17.40.41:8089/api/Master/GetAllCountry')
.subscribe(value => {
}, error => error, () => {
});