Handling HTTP request in an Angular application is a crucial part. In this article we will go through below methods from RxJs to handle HTTP requests.

  1. map → applies the function supplied to each value of input observable and emits the resulting value as observable.
  2. mergeMap or flatMap → allows us to flatten multiple separate subscribed Observable into a single cohesive stream, which we can use to control events coming from user input and from server responses.
  3. switchMap → Projects each source value to an Observable which is merged in the output Observable, emitting values only from the most recently projected Observable.
  4. forkJoin → Accepts an Array of ObservableInput or a dictionary Object of ObservableInput and returns an Observable that emits either an array of values in the exact same order as the passed array, or a dictionary of values in the same shape as the passed dictionary.

map

Use case: One use case could be we want to convert all observables into observables of JSON objects.

signature: map<T, R>(project: (value: T, index: number) => R, thisArg?: any): OperatorFunction<T, R>

#angular #front-end-development #rxjs #http-request

Handling HTTP Request using RxJs in Angular
1.75 GEEK