Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import { Injectable } from '@angular/core';
- import { HttpClient, HttpErrorResponse } from '@angular/common/http';
- import { catchError, map } from 'rxjs/operators';
- import { Observable, of, Subject } from 'rxjs';
- import { environment } from 'src/environments/environment';
- @Injectable()
- export class UsersService {
- private currentUser$ = new Subject<any>();
- private readonly usersEndpoint =
- `${environment.apiUrl}/HttpTrigger2?code=ovABlV0NYCSyUoqLBMiRlCszgOayMoaPi6urrNSqg9N6Kedxaxy0sg==`;
- get currentUser(): Observable<any> {
- return this.currentUser$.asObservable();
- }
- constructor(private httpClient: HttpClient) {
- }
- login() {
- this.currentUser$.next({ id: '', name: 'Roman', age: 22 });
- }
- getUserName(): Observable<string> {
- this.login();
- return this.httpClient.get<any>(this.usersEndpoint).pipe(
- map((user: any) => user.Name),
- // Uncomment the line below to demonstrate forkJoin
- // delay(5000),
- catchError((response: HttpErrorResponse) => {
- console.log('Something went wrong', response);
- return of('No name');
- // Another approach
- // return throwError(response);
- })
- );
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement