RxJS Operators - Explanation, Real World Use Cases, and Anti Patterns
Why Stack Overflow recommends it
Stack Overflow users link it when explaining the differences between switchMap, concatMap and mergeMap, and to cite Ben Lesh's advice that concatMap is usually the right choice for HTTP calls.
Recommended by 3 people across 6 different months, 2019 to 2021.
What it is
RxJS core contributor Ben Lesh and Tracy Lee walk through the main higher-order mapping operators (switchMap, concatMap, mergeMap, exhaustMap) plus merge, map and expand. They cover real-world use cases, how the operators differ, when not to use each, and how many inner observables each subscribes to.
What people said
Recommends timdeschryver · 2019-01 · Stack Overflow · on “NGRX bulk effect of already defined single effect”
This is because you're using the switchMap operator which will cancel the current running observable, in your case the service call. You'll have to use concatMap or mergeMap . If the order is important use concatMap , if not use mergeMap because this will make your service calls in parallel. For more info, watch this [this video] .
Recommends kos · 2019-03 · Stack Overflow · on “RXJS - Multiple observable calls on array of properties”
@TanguyB, I've just googled for some rxjs resources -- and theres a tonn of tutorials and articles on this theme, actually. Though since I see most of them for the first time -- I cant honestly recommend them. Yet, here are two videos that might help you with a start: RxJS Quick Start with Practical Examples [LINK] and a series of video tuts by RxJS core contributors Ben Lesh and Tracy Lee [this video] . Basically just google around, you'll find what fits you best! ;)
Recommends Picci · 2020-03 · Stack Overflow · on “Removing side effect from observable implementation”
One way could be the following. You start defining a method, closureAddressMethod , which expects address as parameter, like this public closureAddressMethod(address: string) { return this.rpc.call('', 'smsglocalkeys', [])).pipe( map((keys: SmsgLocalKeysResult) => [...keys.smsg_keys, ...keys.wallet_keys]), filter((keys: Array<SmsgLocalKey>) => !keys.some(k => k.address === _address)), mergeMap(() => this.rpc.call('', 'smsggetpubkey', [_address])), mergeMap((pubkeyData: PublicKeyData) => this.rpc.call('', 'smsgaddaddress', [ pubkeyData.address, pubkeyData.publickey ])), mergeMap(() => this.rpc.call('', 'smsgaddlocaladdress', [_address])) ) } and then you use this method within the pipe of get
Recommends Picci · 2020-10 · Stack Overflow · on “How to concat more then two observables?”
@Koraxos According to Ben Lesh the main architect behind RxJs ( see this video [this video] ) `concatMap` is the operator to use most of the times with http.
Recommends Picci · 2020-12 · Stack Overflow · on “How can I have an observable call another one based on the result?”
@Jean-Xavier Raynaud I agree with you, IT DEPENDS. At the same time my comment about `concatMap` as the usually right operator for http calls derives from my experience and from a comment of Ben LEsh, the main contributor of rxjs, which you can listen at the beginning of this video [this video] . The preference for `concatMap` is stronger with POST calls, since it ensures you receive the response from each call before doing anything, which helps avoiding multiple POST in fly at the same time.
Recommends Picci · 2021-01 · Stack Overflow · on “Using RxJS to remove nested callbacks when things must be done in sequence”
My comment about `concatMap` as the usually right operator for http calls derives from my experience and from a comment of Ben Lesh, the main contributor of rxjs, which you can listen at the beginning of this video [this video] . The preference for `concatMap` is stronger with POST/PUT calls, since it ensures you receive the response from each call before doing anything, which helps avoiding multiple POST/PUT in fly at the same time.