Reputation: 29
My backend server does not notify the client with any new data. so it's only based on traditional request/response.
Can i turn all my services to use promises instead of observable and subscribers?
for example, i want my service to look like:
import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
@Injectable({
providedIn: 'root',
})
export class HeroService {
restEndpoint: string;
constructor(private http: HttpClient) {
this.restEndpoint = 'entity'
}
get(id?: number): Promise<Entity> {
return this.http.get<Entity>(environment.apiEndpoint + restEndpoint);
}
post(entity: Entity): Promise<Entity> {
return this.http.post<Entity>(environment.apiEndpoint + restEndpoint);
}
put(id: number, entity: Entity): Promise<Entity> {
return this.http.post<Entity>(environment.apiEndpoint + restEndpoint);
}
delete(id: number): Promise<any> {
return this.http.delete<any>(environment.apiEndpoint + restEndpoint);
}
}
( functions returns promises)
Upvotes: 2
Views: 69
Reputation: 9124
Just call toPromise()
after each request
import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
@Injectable({
providedIn: 'root',
})
export class HeroService {
restEndpoint: string;
constructor(private http: HttpClient) {
this.restEndpoint = 'entity'
}
get(id?: number): Promise<Entity> {
return this.http.get<Entity>(environment.apiEndpoint + restEndpoint).toPromise();
}
post(entity: Entity): Promise<Entity> {
return this.http.post<Entity>(environment.apiEndpoint + restEndpoint).toPromise();
}
put(id: number, entity: Entity): Promise<Entity> {
return this.http.put<Entity>(environment.apiEndpoint + restEndpoint).toPromise();
}
delete(id: number): Promise<any> {
return this.http.delete<any>(environment.apiEndpoint + restEndpoint).toPromise();
}
}
PS.: You btw used post within put
Upvotes: 2