What is the best way to make requests?
I have an http request method in project.service.ts. I can make the request with the following structure:
getProjectById(projectId: number) {
return this.http.get<Project>(`${this.url}/search/projects/${projectId}`).toPromise();
}
And call it with the following structure in project.component.ts:
async getProject(projectId: number) {
this.isLoading = true;
try {
this.project = await this.api.projects.getProjectById(projectId);
} finally {
this.isLoading = true;
}
}
But I can also make the request like this:
async getProjectById(projectId: number) {
return await this.http.get<Project>(`${this.url}/search/projects/${projectId}`).toPromise();
}
Or this:
getProjectById(projectId: number): Observable<Project> {
return this.http.get<Project>(`${this.url}/search/project/${projectId}`);
}
But I'd have to subscribe to this function.
This is rather question of design of code and clean code, than efficiency. I reckon following the guidelines of Angular built-in Http module.
You need to keep the code clean, and follow the RxJS flow, rather than convert it to async code.
So you need to use this:
getProjectById(projectId: number): Observable<Project> {
return this.http.get<Project>(`${this.url}/search/project/${projectId}`);
}
If you have no reason, don't complicate it.