我正在使用 RxJS。我有两个 Observables(API 调用),它们为同一个数组返回不同数量的信息。我想在 Observables 之间切换并让订阅识别这两个结果,即每次返回值时触发。
这是一个使用 Typescript 的示例(这不起作用):
let foo = [];
const callOne$ = this.service.getSomeData$; // this should take 0.5 seconds
const callTwo$ = this.service.getAllTheData$; // this should take 6 seconds
callOne$.pipe(
switchMap(data => data.length > 0 ? callTwo$ : of(data)
).subscribe(data => {
console.log(data); // this fires once but I want it to fire twice
foo = data;
});
上面的例子先检索callOne$
,然后检索callTwo$
,然后给我结果。相反,我希望两者的结果都按顺序排列。我将如何订阅 Observables 以便接收第一个结果,然后通过第二个调用进行更新?