我想一個接一個地運行一組可觀察的,因為每個結果都取決于前一個。但是,最后我還需要所有中間結果,因為它們是在我們使用forkJoin- 在陣列中時給出的。
我有以下代碼:
getData$ = getResponses$
.pipe(
switchMap((data_1) => {
return getResponse_1$;
})
)
.pipe(
switchMap((data_2) => {
return getResponse_2$;
})
);
getResponse_2$ 取決于 getResponse_1$ 和 getResponse_1$ 取決于 getResponses$
我希望最終結果,即 getData$ 成為中間結果的陣列(如forkJoin)。有沒有這樣的方法?
uj5u.com熱心網友回復:
您可以嘗試以下方法:
// Let's consider that you have the following observables:
const getResponses$ = of('Text');
const getResponse_1$ = of(1);
const getResponse_2$ = of(true);
// The type of getData$ will be: Observable<[data: string, data_1: number, data_2: boolean]>
const getData$ = getResponses$.pipe(
switchMap((data) =>
getResponse_1$.pipe(map((data_1) => ({ data, data_1 })))
),
switchMap(({ data, data_1 }) =>
getResponse_2$.pipe(
map((data_2) => {
const res: [data: string, data_1: number, data_2: boolean] = [
data,
data_1,
data_2,
];
return res;
})
)
)
);
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/436130.html
