Advertisement
Guest User

Untitled

a guest
Oct 24th, 2019
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.20 KB | None | 0 0
  1. import { Injectable } from '@angular/core';
  2. import { HttpClient, HttpErrorResponse } from '@angular/common/http';
  3. import { catchError, map } from 'rxjs/operators';
  4. import { Observable, of, Subject } from 'rxjs';
  5.  
  6. import { environment } from 'src/environments/environment';
  7.  
  8. @Injectable()
  9. export class UsersService {
  10. private currentUser$ = new Subject<any>();
  11.  
  12. private readonly usersEndpoint =
  13. `${environment.apiUrl}/HttpTrigger2?code=ovABlV0NYCSyUoqLBMiRlCszgOayMoaPi6urrNSqg9N6Kedxaxy0sg==`;
  14.  
  15. get currentUser(): Observable<any> {
  16. return this.currentUser$.asObservable();
  17. }
  18.  
  19. constructor(private httpClient: HttpClient) {
  20. }
  21.  
  22. login() {
  23. this.currentUser$.next({ id: '', name: 'Roman', age: 22 });
  24. }
  25.  
  26. getUserName(): Observable<string> {
  27. this.login();
  28. return this.httpClient.get<any>(this.usersEndpoint).pipe(
  29. map((user: any) => user.Name),
  30. // Uncomment the line below to demonstrate forkJoin
  31. // delay(5000),
  32. catchError((response: HttpErrorResponse) => {
  33. console.log('Something went wrong', response);
  34. return of('No name');
  35.  
  36. // Another approach
  37. // return throwError(response);
  38. })
  39. );
  40. }
  41. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement