How to cancel/unsubscribe all pending HTTP requests in Angular 4+

Checkout the takeUntil() operator from RxJS to globally drop your subscriptions : – RxJS 6+ (using the pipe syntax) import { takeUntil } from ‘rxjs/operators’; export class YourComponent { protected ngUnsubscribe: Subject<void> = new Subject<void>(); […] public httpGet(): void { this.http.get() .pipe( takeUntil(this.ngUnsubscribe) ) .subscribe( (data) => { … }); } public ngOnDestroy(): void { … Read more

Behaviour subject initial value null?

The purpose of BehaviorSubject is to provide initial value. It can be null or anything else. If no valid initial value can be provided (when user id isn’t known yet), it shouldn’t be used. ReplaySubject(1) provides a similar behaviour (emits last value on subscription) but doesn’t have initial value until it is set with next. … Read more

Best way to import Observable from rxjs

Rxjs v 6.* It got simplified with newer version of rxjs . 1) Operators import {map} from ‘rxjs/operators’; 2) Others import {Observable,of, from } from ‘rxjs’; Instead of chaining we need to pipe . For example Old syntax : source.map().switchMap().subscribe() New Syntax: source.pipe(map(), switchMap()).subscribe() Note: Some operators have a name change due to name collisions … Read more

flatMap, mergeMap, switchMap and concatMap in rxjs?

Taking this from a previous answer: flatMap/mergeMap – creates an Observable immediately for any source item, all previous Observables are kept alive. Note flatMap is an alias for mergeMap and flatMap will be removed in RxJS 8. concatMap – waits for the previous Observable to complete before creating the next one switchMap – for any … Read more

Angular 2 – Chaining http requests

Chaining HTTP requests can be done using flatMap or switchMap operators. Say we want to make three requests where each request depends on the result of previous one: this.service.firstMethod() .flatMap(firstMethodResult => this.service.secondMethod(firstMethodResult)) .flatMap(secondMethodResult => this.service.thirdMethod(secondMethodResult)) .subscribe(thirdMethodResult => { console.log(thirdMethodResult); }); This way you can chain as much interdependent requests you want. UPDATE: As of RxJS … Read more

Handling refresh tokens using rxjs

From a quick look at your code I would say that your problem seems to be that you are not flattening the Observable that is returned from the refresh service. The catch operator expects that you will return an Observable that it will concatenate onto the end of the failed Observable so that the down … Read more

Rxjs One Observable Feeding into Another

For transforming items emitted by an Observable into another Observable, you probably want to use the mergeMap operator. It creates an inner Observable and flattens its result to the outer stream. const source = this.myService .getFoo() .pipe( mergeMap(result => this.myService.getMoo(result)) ) .subscribe(result2 => { // do some stuff }); Here are some flat operators you … Read more

RXJS Wait for all observables in an array to complete (or error)

If you want to compose an observable that emits when all of the source observables complete, you can use forkJoin: import { Observable } from ‘rxjs/Observable’; import ‘rxjs/add/observable/forkJoin’; import ‘rxjs/add/operator/first’; var tasks$ = []; tasks$.push(Observable.timer(1000).first()); tasks$.push(Observable.timer(3000).first()); tasks$.push(Observable.timer(10000).first()); Observable.forkJoin(…tasks$).subscribe(results => { console.log(results); });

tech