Questions tagged [switchmap]

Questions about switchMap operator, an RxJS flattening operator to handle observables and emit values.

switchMap is an RxJS flattening operator to handle observables and emit values. Used in coding reactive web pages.

This operation works well to handle observables, where there is no need to ensure that the call has succeeded, only returning the value from the latest call.

Related tags:

195 questions
1
vote
1 answer

Keep state while operating in switchMap

Suppose that you have a function that returns an rxjs observable that contains a list of objects. const getItems = () => of([ { id: 1, value: 10 }, { id: 2, value: 20 }, { id: 3, value: 30 …
antoniom
  • 3,143
  • 1
  • 37
  • 53
1
vote
0 answers

RxJS switchMap operator does not cancel http request

I have been struggling to figure out why the HTTP requests are not been canceled using the switchMap operator. import {Observable, of, Subject, Subscription, timer} from 'rxjs'; import {HttpClient} from '@angular/common/http'; constructor(private…
Alex Portnoy
  • 426
  • 1
  • 5
  • 14
1
vote
1 answer

using a function that calls observable with 3 different observables tied together with switch map. results in error

I have a function that runs when a file is uploaded to a html form. uploadidnexttofacepicture(event){ let subscription = this.s3service.publicresourceuploadtos3(event) .subscribe((req: any)=>{ console.log(req); }); …
1
vote
3 answers

How to use switchmap to avoid nested subscriptions in Angular?

I read a lot about switchmap and its purpose but I did not see a lot of examples when it comes to subscribing to the new data. So I use a nested subscription in my Angular project and I wanted to ask you how to use switchmap properly in my example…
Opat
  • 93
  • 1
  • 2
  • 9
1
vote
1 answer

Usage of switchMapTo after switchMapTo

Expected behavior: 1) Get all tasks from taskService.getAllTasks(); 2) Pass the result tasks to filterService.addFilterableTasks(); 3) Get tasks from filterService.getFilteredTasks(); 4) Pass the result tasks to searchService.addTasks(); 5) Get…
Ivan Burzakovskyi
  • 693
  • 1
  • 8
  • 25
1
vote
1 answer

NgRx and RxJS : Dependent State Selector

I have a component that subscribes to multiple slices of state in the constructor. @ViewChild(PComponent) PGrid: PComponent; ngOnInit() { this.store.pipe( select(fromReports.getProcessState), takeWhile(() =>…
Abhi.Net
  • 722
  • 3
  • 11
  • 37
1
vote
1 answer

Force call inner observable of switchMap if outer observable returned an error

My code: return this.userService.getPosition().pipe( switchMap(() => { return this.get('/places', { point: this.userService.coords }); }), ); There can be a case, when the position cannot be retrieved (no https in Google…
Sergej
  • 2,030
  • 1
  • 18
  • 28
1
vote
3 answers

Is There a way to subscribe only to latest response of SwitchMap?

Consider following example: import { fromEvent } from 'rxjs'; import { switchMap, tap } from 'rxjs/operators'; import { ajax } from 'rxjs/ajax'; const httpCall$ = ajax.getJSON('https://rickandmortyapi.com/api/character/'); const click$ =…
billyjov
  • 2,778
  • 19
  • 35
1
vote
1 answer

Why are these two observables emitting different streams?

I am using marble diagrams to show the output of two different observables. The first uses a switchmap, that pipes into another switchmap. The second observable has two switchmaps in the same pipe. Here are the two marble flows: The first uses…
Andre
  • 145
  • 1
  • 11
1
vote
1 answer

rxjs switchMap and tap issue

I was playing around with the switchMap operator to clearly understand what was happening to a "switched" inner observable. At first i thought that switchMap was only "unsubscribing" from the switched inner observable, but then i realize it was in…
Clement
  • 3,860
  • 4
  • 24
  • 36
1
vote
2 answers

Call another HTTp request inside after an HTML request in an angular service

In some angular service, I need to call a PUT method named, I need to call another GET method once after it is completed. Then the results from GET method should be stored in a variable in the service itself. The code which I have written has some…
1
vote
0 answers

Using Promise inside SwitchMap operator

source$.switchMap(x => promise(x)) .subscribe(x => console.log(x)); source$.switchMap(x => from(promise(x))) .subscribe(x => console.log(x)); both the lines of code resolves the promise but can someone really point out what's the different in…
Ashwin J
  • 662
  • 7
  • 12
1
vote
1 answer

SwitchMap usage onTabSelect

Below is my code, when user selects tab, I want to cancel the existing call and load the tab details which user clicks. I understand I have to use switchmap(guide me if it is not right or better alternative) I follow this example and it didnot work…
Chatra
  • 2,989
  • 7
  • 40
  • 73
1
vote
2 answers

waiting for multiple observables (in parallel) to complete in an Angular effect

I am writing an effect that requires the results of several separate service calls (returning as observables) before completing it. The first service call must complete before 3 more (which can be async), and then I build my action list. I have it…
Kim Gentes
  • 1,496
  • 1
  • 18
  • 38
1
vote
1 answer

switchMap Angular /

I upgraded my angular to ng7. I'm working on a project, where I have 3 http calls, which are depended on each other, so i choose to use switchMap. My problem is that, the way I wrote switchMap before doesn't work. This is how usually wrote my…
EdwinS95
  • 138
  • 11