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
35
votes
4 answers

Rxjs conditional switchMap based on a condition

I have a situation like the following: myObservable1.pipe( switchMap((result1: MyObservable1) => { if (condition) { return myObservable2; } else { return of(null); } }) ).subscribe(myObservable1 | myObservable2) => { So…
user2010955
  • 3,871
  • 7
  • 34
  • 53
8
votes
3 answers

RxJs switchMap with angular HttpClient

I have a use case whenever a new request is triggered, any http requests that are already in flight should be cancelled / ignored. For eg: A request (say #2) comes in while the request #1 takes too long to respond / slow network connectivity.…
mperle
  • 3,342
  • 8
  • 20
  • 34
7
votes
2 answers

Why does switchMap operator only emit the last value when used with a promise?

I have some trouble to understand this. When I use the switchMap operator with an Observable it emits all the values as expected: Observable.from([1, 2, 3, 4, 5]) .do(console.log) .switchMap(i => Observable.of('*' + i)) .do(console.log) …
Martin
  • 123
  • 9
6
votes
1 answer

How to cancel http request with switchMap?

I want to cancel the previous http request if i made the new http request to server but it is not working as expected to me. i want to cancel the request if some value got changed in the function i mean if the value change the old request should…
Ankit0827
  • 63
  • 1
  • 1
  • 3
6
votes
1 answer

RXJS - SwitchMap or SwitchMapTo for resetting timer

So I am new to RXJS. What I am trying to do is setup a session expiration timer, and when a user receives a modal prompt that their session is about to expire, if they click Continue, the timer is reset. I've been reading on switchMap and…
5
votes
2 answers

Angular - How to implement switchMap for multiple http requests?

In my project, I sometimes want to update the available options in a list when the user changes the selected value in another list. To do this, I've used valueChanges with the pipe operator and switchMap like…
Chris
  • 1,417
  • 4
  • 21
  • 53
5
votes
1 answer

NgRx effect with switchMap and catchError - can someone explain the difference in observable workflows between my code and the 'correct' one?

I sent the below code snippet for code review. This effect needs to dispatch a success action after a request call, or an error action if the service method throws an error, so pretty standard. @Effect() fetchData$ = this.actions$.pipe( …
Ida Štambuk
  • 53
  • 1
  • 5
5
votes
1 answer

rxjs switchMap not working in Angular Guard?

return this.auth.user.pipe( switchMap((user: IUser) => { return of(true); }) ); My initial code was a bit more complex with some cases depending on the user data, but for testing purposes I've used the code above in a…
SebastianG
  • 8,563
  • 8
  • 47
  • 111
5
votes
2 answers

Understanding SwitchMap in rxjs

as per the definition of the switchMap On each emission the previous inner observable (the result of the function you supplied) is cancelled and the new observable is subscribed. You can remember this by the phrase switch to a new …
Lijin Durairaj
  • 4,910
  • 15
  • 52
  • 85
5
votes
3 answers

angular async reloading spinner

I have a simple setup to show a loading spinner when the async pipe is null:
loading.. However, when the user searches again for…
Rusty Rob
  • 16,489
  • 8
  • 100
  • 116
4
votes
1 answer

Typescript: error TS2740: Type '{}' is missing the following properties from type 'Product[]': length, pop, push, concat, and 26 more

Using Angular 4, I am getting array list from firebase db inside an observable. How to change the type of an observable to an array or vice versa to make it work? There is no different function that i have written. I am using an inbuilt function…
Pinka
  • 165
  • 1
  • 3
  • 12
4
votes
1 answer

How to fix "Required MutableLiveData but 'SwitchMap' was inferred to LiveData" when using Transformations.switchMap?

I am trying to use ViewModel with some LiveData that uses the value from another LiveData. For that I am trying to use Transformations.switchMap but I am getting Incompatible types. Required MutableLiveData but 'switchMap' was inferred to…
4
votes
3 answers

how to use switchMap to cancel pending http requests and taking the last subscribe only?

i tried canceling pending http request using subscription.unsubsribe like this: getAgentList(pageNumber: number, filter: string): any { let requestUrl: string = 'api/service/agents_search?ACCT=' +this.accountId; if ( this.subscription ) { …
Shaul Naim
  • 246
  • 3
  • 15
3
votes
3 answers

RxJS: Handle cancelled events when using `switchMap` operator

Consider the following snippet const { NEVER, timer } = rxjs; const { catchError, switchMap, timeout } = rxjs.operators; timer(0, 3000).pipe( switchMap(() => timer(randomIntFromInterval(1, 4) * 1000).pipe( // <-- mock HTTP call …
ruth
  • 29,535
  • 4
  • 30
  • 57
3
votes
2 answers

switchMap combined with mergeMap

I have an Observable where each new value should cause an HTTP request. On the client-side I only care about the latest response value; however, I want every request to complete for monitoring/etc. purposes. What I currently have is something…
Steve D
  • 373
  • 2
  • 17
1
2 3
12 13