Reputation: 47
I make several sequential requests and the result of anotherRequest
is used in the second mergeMap
-clause. This basically works fine.
My question is: How can I repeat anotherRequest
until some condition is met and only pass the approved return-value to the second mergeMap
-clause?
someRequest().pipe(
mergeMap((info) => {
this.appService.petitionId$.next(Number(lastInfo.petitionId));
return anotherRequest();
}),
mergeMap(
...
),
);
Upvotes: 0
Views: 176
Reputation: 4585
You could use expand
in combination with filter
and take(1)
. By using the expand
-operator the call will be repeated until a given condition is met. filter
will make sure that an emission will only be made if the condition was met. And take(1)
will trigger an unsubscribe after the first successful emission.
Here you can find my stackblitz example
What's more, you can find the crucial part of my code below:
this.someRequest()
.pipe(
mergeMap((info) => {
return this.anotherRequest().pipe(
expand((data) =>
this.isConditionMet(data) ? of(data) : this.anotherRequest()
),
filter((data) => this.isConditionMet(data)),
take(1)
);
}),
mergeMap((res) => {
...
})
)
Upvotes: 3