I am working on an Angular(v4) application. Like any other web app, this app also has HTTP requests and for each of these requests, I have a subscribe to get the data and show them on the UI. I have been reading about of use of async pipe and why it is better than subscribe. From what I have read, it surely has benefits over normal subscribe method and I am convinced that I should use it in my application but I am not sure how to structure my app with this aync pipe instead of subscribe. Let me post the structure of my application:
I have a service layer that issues the Http request to backend, something like:
some-service.component.ts
@Injectable()
export class SomeService {
constructor(private http: HttpClient) {
}
getSomething() {
return this.http.get(`${this.baseUrl}/${'test'}`);
}
}
The above service provides an Observable to my component, where I usually subscribe to it as shown below:
some-component.ts
@Component({
selector: 'some-app',
templateUrl: './some.component.html',
styleUrls: []
})
export class SomeComponent {
constructor(private someService: SomeService) {
}
getSomething() {
this.someService
.getSomething()
.subscribe(
// the first argument is a function which runs on success
(data) => {
// DO SOME DATA TRANSFORMATION HERE
},
// the second argument is a function which runs on error
(err: HttpErrorResponse) => {
this.exceptionService.errorResponse(err);
},
// the third argument is a function which runs on completion
() => {
// MAKE SOME FUNCTION CALL
}
);
}
}
Above is a general structure as how I do a subscribe for a general HTTP response. I want to get rid of this subscribe and use async instead. However, my issue is, I do some data transformation upon receiving the data (as shown above) or sometime I make some function call from thecompletionobserver (as shown above) and with such operations, I am not sure as how I am going to these operations once switch to async.
For data transformation, I guess, I can do it in my service (not sure though) but how can I make function call as I do it now.
Any input would be highly appreciated. Also let me know if my question needs more clarifications. Thanks!