Skip to content

Instantly share code, notes, and snippets.

@ivankisyov
Last active April 8, 2019 04:03
Show Gist options
  • Save ivankisyov/75c867172b60630e4b153f178757cb61 to your computer and use it in GitHub Desktop.
Save ivankisyov/75c867172b60630e4b153f178757cb61 to your computer and use it in GitHub Desktop.
RxJS Operators

RxJS Operators

  • catch - dealing with errors
delete(id) {
  return this.http.delete(this.url + '/' + id)
          .catch(this.handlerError)
}
  • toPromise - use promise instead of observable(the observable is converted to a promise)
delete(id) {
  return this.http.delete(this.url + '/' + id)
          .toPromise()
          .catch(this.handlerError);
}
  • retry - will retry the request {n} times
delete(id) {
  return this.http.delete(this.url + '/' + id)
          .retry(3)
          .catch(this.handlerError);
}
  • switchMap - when dealing with multiple async operations
// switch from one observable to another
ordersService
  .getAll()
  .switchMap(orders => {
    this.orders = orders;
    return route.queryParamMap // here we're switching to this new observable that gets returned
  })
  .subscribe(params => {
    ....
  })
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment