[英]RXJS - Are nested concapMap equivalent to sequential concatMap?
[英]Rxjs make sequential calls using concatMap
我想進行兩次順序調用(如果第一次完成,請調用第二次):
我的代碼是這樣的:
myApiClient
.firstApiCall()
.pipe(take(1),
concatMap (firstResult => {
doSomethingWithResponse(firstResult);
return myApiClient.secondApiCall();
}), take(1))
.subscribe(secondResult => {
doSomethingWithResponse(firstResult);
}, error => {
catchSecondApiCallError(error);
});
第一個問題:這是進行順序調用的正確方法嗎? 第二個問題:我如何才能捕捉到第一次通話的錯誤?
是的,這是使用 rxjs 鏈接順序 api 調用的正確方法。 對於第二個問題,您可以使用catchError
運算符。
myApiClient
.firstApiCall()
.pipe(
take(1),
catchError(e => {
console.log('Error caught from first api ', e);
return EMPTY;
}),
concatMap (firstResult => {
doSomethingWithResponse(firstResult);
return myApiClient.secondApiCall();
}), take(1))
.subscribe(secondResult => {
doSomethingWithResponse(secondResult);
}, error => {
catchSecondApiCallError(error);
});
EMPTY 是從以下位置導入的:
import { EMPTY } from 'rxjs';
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.