I want to know that do we really need .map
when calling any api using http
in Angular 2
?
Please check my below code. It is working fine with .map
and even without .map
. If api returns data then it will return success else it will return error. I will also return any model data from here after performing some action. So, do I need Observable
? Is there any benefit of using it ? I am using .subscribe
at component
side to receive data. Is this fine or do I need any improvement ?
returnData: ReturnData;
callyAPI(body: modelData) {
return this.http.post(URL, body)
.do(data => {
for (let i = 0; i < data.length; ++i) {
this.returnData.push(data[i]);
}
return this.returnData;
},
error => {});
});
}
You don't need to use map
but do
is definitly the wrong operator here
do
is supposed to execute some code for every event, but not to modify the events value, while map
can update or replace the event by a different value like you do in your example.
https://github.com/ReactiveX/rxjs/blob/master/src/operator/do.ts#L13-L14
- Perform a side effect for every emission on the source Observable, but return
- an Observable that is identical to the source.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.