Angular 2 observable subscribing twice executes call twice

You may use the share operator on your result from HttpClient.get, like this: var observable = this.httpClient.get(‘api/version/secured’, { headers: httpHeaders }) .pipe(share()); You’ll need to add the following import on top of your script: import { share } from ‘rxjs/operators’; The share operator makes an observable hot, i.e. shared between subscribers. But there’s a lot … Read more

Observables : Cancel previous http request on new subscription call

I would use a subject to keep everything reactive. In your template html listen to change events and emit a new value to the subject. <searchBar (change)=”search$.next($event.target.value)” /> then in your component: this.subscription = this.search$.pipe( debounceTime(800), distinctUntilChanged(), switchMap(searchText=>http.post(‘api_link’, {searchText}) }).subscribe(response=>{ this.response = response. }); The switchMap will cancel any HTTP request that hasn’t completed if … Read more

tap() vs subscribe() to set a class property

Edit: Ignore this answer! Here is a good answer: https://stackoverflow.com/a/50882183/5932590 See JMD’s comment below for more context! Good question. In the source code for the tap operator, this comment pretty much sums it up: This operator is useful for debugging your Observables for the correct values or performing other side effects. Note: this is different … Read more

Angular 2: How to call a function after get a response from subscribe http.post

Update your get_categories() method to return the total (wrapped in an observable): // Note that .subscribe() is gone and I’ve added a return. get_categories(number) { return this.http.post( url, body, {headers: headers, withCredentials:true}) .map(response => response.json()); } In search_categories(), you can subscribe the observable returned by get_categories() (or you could keep transforming it by chaining more … Read more

RxJS Observables nested subscriptions?

As mentioned in comments, you are looking for the flatMap operator. You can find more details in previous answers : How to do the chain sequence in rxjs Why do we need to use flatMap? Your example would read as : this.returnsObservable1(…) .flatMap(success => this.returnsObservable2(…)) .flatMap(success => this.returnsObservable3(…)) .subscribe(success => {(…)});

Angular 2 – Checking for server errors from subscribe

As stated in the relevant RxJS documentation, the .subscribe() method can take a third argument that is called on completion if there are no errors. For reference: [onNext] (Function): Function to invoke for each element in the observable sequence. [onError] (Function): Function to invoke upon exceptional termination of the observable sequence. [onCompleted] (Function): Function to … Read more

Subscribe is deprecated: Use an observer instead of an error callback

subscribe isn’t deprecated, only the variant you’re using is deprecated. In the future, subscribe will only take one argument: either the next handler (a function) or an observer object. So in your case you should use: .subscribe({ next: this.handleUpdateResponse.bind(this), error: this.handleError.bind(this) }); See these GitHub issues: https://github.com/ReactiveX/rxjs/pull/4202 https://github.com/ReactiveX/rxjs/issues/4159

tech