2

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)

11
  • 1
    My backend server does not notify the client with any new data. so it's only based on traditional request/response. That does not mean you should abandon observables. The httpclient returns 'cold' observables, which means they are not continuing to emit data, but just emit data once. Why do you want to force the use of Promises? Commented Nov 22, 2019 at 10:12
  • 2
    @Silvermind that's not what "cold" means. "cold" means that the observable doesn't emit anything until it's subscribed, and that each subscription triggers the process which emits (i.e. sends a request). Commented Nov 22, 2019 at 10:14
  • 1
    In short Observable are good to use with angular applications. Commented Nov 22, 2019 at 10:22
  • 1
    No. All observables automatically unsubscribe their observer once they complete or error. Commented Nov 22, 2019 at 11:08
  • 1
    For reference: When an Observable issues an OnError or OnComplete notification to its observers, this ends the subscription. Observers do not need to issue an Unsubscribe notification to end subscriptions that are ended by the Observable in this way. this is from reactivex.io/documentation/contract.html Commented Nov 22, 2019 at 11:19

1 Answer 1

2

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

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.