0

I am chaining HTTP calls as per https://stackoverflow.com/a/34107312/470014 however I only want to execute the next call if the previous one returns null. So I have the following code:

public getTolerances() {
  this.queryServer(12,34).pipe(
    mergeMap(previous => {
        if (previous) {
            return of(previous);
        } else {
            return this.queryServer(0,34);
        }
    }),
    mergeMap(previous => {
        if (previous) {
            return of(previous);
        } else {
            return this.queryServer(34,0);
        }

    }),
    mergeMap(previous => {
        if (previous) {
            return of(previous);
        } else {
            return this.queryServer(0,0);
        }
    })
  )
  .subscribe((result) => {
      console.log({result});
  });
}

If the previous call returns a result I am using of operator to return that as an observable. Is that necessary or is there a way I can drop them? It feels like I am unwrapping the promise and then wrapping it up again but if I just return previous I get the following type error:

Argument of type '(result: unknown) => unknown' is not assignable to parameter of type '(value: unknown, index: number) => ObservableInput'. Type 'unknown' is not assignable to type 'ObservableInput'.ts(2345)

Caltor
  • 2,538
  • 1
  • 27
  • 55
  • For the type error that's straightforward and the compiler is correct since result's type and the operator argument's type are not compatible. Curious, when you do: if (previous) does you know by default that previous is nullish or it is typo, since if you want to check whether it is nullish or not it should be if (!previous) – onrails Sep 06 '22 at 14:56
  • @onrails ok, thanks for the confirmation. I know it's null if no answer is returned as I wrote the backend too. If it's truthy (not null) then I return the result of the previous http call. Otherwise (else) I query the next potential value. If I use !previous I would have to reverse the clauses. Admittedly I could remove the `else` and just `return` in the mainline code, in which case it might make more sense to invert the `if` statement as you suggest... – Caltor Sep 06 '22 at 15:11
  • Go it! So in other words you want to skip the current API endpoint call to the next call based on a supplied predicate ? – onrails Sep 06 '22 at 15:17
  • @onrails erm I think so. I only want to make the next call if the current/previous call returns null. Are we saying the same thing, just in different words? Maybe there is a better way of doing this in RxJS? – Caltor Sep 06 '22 at 15:18
  • Yep, with RxJs switchMap you can have something similar to what is above. mySourceObservable .pipe( switchMap(val => { if(val === a) { return Obs1 } else if(val === b) { return obs2 } else { return Obs3 } }) ).subscribe(); – onrails Sep 06 '22 at 15:32
  • @onrails Could you post that as an answer please so I can see the formatting properly and give you the kudos if it works. – Caltor Sep 06 '22 at 16:34

0 Answers0