如果我没理解错的话,在map
操作员中"输入"catchError
应该可以帮你解决这个问题.
沿着这些思路的一些东西
http1$.pipe(
switchMap((response1) => http2$.pipe(
map(() => response1)).pipe(
catchError(() => of(response1))
)
)
).subscribe(response1 => {
// I need to get response 1 even if http2 fails which, in this case, won't work
console.log(response1);
});
EDIT AFTER THE COMMENT个
相反,如果您想在Http1$返回后触发并忘记Http2$,但对Http2$的结果(忘记部分)完全不感兴趣,并立即返回Http1$的响应,则可以这样进行
// first create a shared stream starting with http1$
const http1Shared$ = http1$.pipe(
share()
);
// then use the shared stream to create a new stream which will trigger http2$ as soon as http1$ returns
// the use of the shared stream is to make sure http1$ is only called once
// you can catch the error and ignore it using catchError(() => EMPTY)
// EMPTY is an observable that completes immediately without emitting any value and without erroring
const stream2$ = http1Shared$.pipe(
switchMap(() => http2$),
catchError(() => EMPTY),
);
// then subscribe to both streams - the first one will be used to process the response from http1$
// the second one will be used to trigger http2$ as soon as http1$ returns but will ignore its response
http1Shared$.subscribe(response1 => {
// I need to get response 1 even if http2 fails which, in this case, won't work
console.log(response1);
});
stream2$.subscribe();