0

I have an array of requests. I want to make the first request and check whether the response ({active: true/false}) is true or false. If the response has active: true, do not send other requests and return info. If the response has active: false, send another request and make the same check.

fakeChildService(child: Child): Observable<{active: boolean, info: string}>
{...}


let requests = [];
let children = action['children'];
children.map((child: Child) => {
   requests.push(this.myService.fakeChildService(child));
})
arunkumaraqm
  • 337
  • 1
  • 13
  • Does this answer your question? [Repeat a request inside mergeMap, until its return value meets a certain condition](https://stackoverflow.com/questions/75260425/repeat-a-request-inside-mergemap-until-its-return-value-meets-a-certain-conditi) – Vikas Jun 16 '23 at 04:47
  • https://stackoverflow.com/questions/68686727/use-rxjs-to-keep-triggering-http-calls-in-angular-until-a-condition-is-met – Vikas Jun 16 '23 at 04:48
  • https://stackoverflow.com/questions/73398235/repeat-http-call-until-the-desired-value-is-returned-with-rxjs – Vikas Jun 16 '23 at 04:49
  • Check out expand operator, that's the one you want – maxime1992 Jun 16 '23 at 04:56

2 Answers2

0

You can use the repeatWhen operator along with other operators to conditionally repeat the request based on the response data. For example:

import { from, interval, of } from 'rxjs';
import { concatMap, repeatWhen, takeUntil, filter } from 'rxjs/operators';

 

fakeChildService(child: Child): Observable<{active: boolean, info: string}>
{...}
 
GetSatisfactoryData(child:any) {

    const interval$ = interval(2000);
    interval$
      .pipe(
        concatMap(() => from(this.fakeChildService(child))),
        filter((data) => data.active),
        takeUntil(interval$.pipe(repeatWhen(() => interval$)))
      ).subscribe((data) => {
        console.log('found:', data.info);
      });
  }
Abdullah Qudeer
  • 949
  • 7
  • 24
0

We ended up calling the service recursively for the children one by one and stopping the recursion when the satisfactory data is found.

arunkumaraqm
  • 337
  • 1
  • 13