Reputation: 63
I am working in Angular 2, and I am making a series of async calls from a service. Some of the calls only need to be made conditionally in my call chain. My initial call chain looks like the following:
PSEUDO CODE
this.post().flatMap( () => this.put() ).flatMap( () => this.get() )..etc.
Then I need to conditionally attach additional async calls to my chain based on a variable array.
I'm using this kind of approach:
PSEUDO CODE
...flatMap( () => return this.additionalCallsFunction(callArray) )
.flatMap( () => this.finalPostRequest() ).subscribe(...)
additionalCallsFunction(callArray){
if(callArray.length === 0) return Observable.empty()
else { return this.get().concatMap( (res) => this.put(res).flatMap( () => {
callArray.removefirstItem()
return this.additionalCallsFunction(callArray)
});
}
I'm fairly new to Angular and Observables, so I'm not sure I'm approaching this correctly. Whether or not my additional Calls Array is empty or not I'm seeing my initial calls happen, but neither the additional calls, nor my final Post request ever goes through when my function returns. I'm also not seeing any errors in my console. I've tried replacing .empty() with .never(), but no change.
Any help/advice appreciated!
Upvotes: 4
Views: 866
Reputation: 495
I had the same situation, and I finished with using Observable.of(undefined)
instead of Observable.empty()
.
I don't know why, but Observable.empty()
interrupts the chain.
Any further explanation is very appreciated.
Upvotes: 1