This is like calling multiple consecutive HTTP requests where results depend on each other. This is where you want to use concatMap()
instead because it'll wait until the Observable returned from the callback function completes.
It's hard to tell what exactly you want to do from your description but if you need to stop propagating the results (and avoid calling unnecessary HTTP requests) have a look takeWhile()
operator or simple filter()
operators.
With filter()
:
return observable
.concatMap(response => response)
.filter(response1 => response1.whatever === true)
.concatMap(response1 => observable1(response1))
.filter(response2 => response2.whatever === true)
.concatMap(response2 => observable2(response2))
.filter(response3 => response3.whatever === true)
This simply won't propagate the item further if it fails the filter
test. However the source observable
can still emit values that'll go through the chain. In other words the filter
operator doesn't complete the chain.
With takeWhile()
:
return observable
.concatMap(response => response)
.takeWhile(response1 => response1.whatever === true)
.concatMap(response1 => observable1(response1))
.takeWhile(response2 => response2.whatever === true)
.concatMap(response2 => observable2(response2))
.takeWhile(response3 => response3.whatever === true)
If any of the takeWhile()
result into false it'll complete the chain and no other value can be emitted.
response
, does the second condition depend onresponse2
etc? – Bergiresponse2
– Joel Raju