RxJS 如何一个接一个地向服务器发送请求?

How to send requests to server one after one RxJS?

如何正确发送 RxJS 请求,当有 4 个不同的 API 端点时,我需要请求每个端点直到之前没有 return 数据。

架构是:

Request 1 -> Return Void
Request 2 -> Returned data, stop working and return data
Request 3 -> Will fail

结果为:

其中一个请求只有一个结果。

我试过这个:

req1$ = of(response);
req2$ = of(response);

req1$.pipe( flatMap((result) => {
   if (result) { return of(result); } else {return of([]);}
}));

我认为你需要做一个手动链,比如:

const getValueFromServer$  = req1$.pipe(flatMap => result ? of(result) : firstFallback$);
const firstFallback$       = req2$.pipe(flatMap => result ? of(result) : secondFallback$)
const secondFallback$      = req3$.pipe(flatMap => result ? of(result) : req4$)

getValueFromServer$.subscribe(console.log)