我想知道當 rxjs 過濾條件的條件不正確時是否可以捕獲。
這就是我所擁有的:
of(1)
.pipe(
map((d) => d 1),
filter((d) => d === 0),
map((d) => d 1), // this will go in because of the filter
)
.toPromise()
.then((d) => console.log(d)) // display indefined
.catch(() => console.log("ERRRRRROOOOOOR")) // do not display :(
在這種情況下,我想在過濾條件不正確時回傳特定資料(或至少拋出)。
我試圖將引數添加到過濾器
filter(filter((d) => d === 0), "john doe")
但這不顯示字串,
我想 fp-ts 中的等價物是捕捉左邊,但我不知道你是否可以使用 rxjs 進行函式式編程,或者你是否只能使用 observable。您可以使用管道和多個運算子這一事實可以在我的專案中使用此庫,因此最好避免更改庫
非常感謝 !
uj5u.com熱心網友回復:
你需要拋出一個錯誤才能捕捉到它!
of(1)
.pipe(
map((d) => d 1),
switchMap((d) => {
if (d === 0) {
return of(d 1);
}
throwError(() => new Error('Erroooooor'));
})
)
.toPromise()
.then((d) => console.log(d)) // display indefined
.catch(() => console.log('ERRRRRROOOOOOR')); // thrown if d !=== 0
也可以使用iif運算子撰寫
of(1)
.pipe(
map((d) => d 1),
switchMap((d) =>
iif(
() => d === 0,
of(d 1),
throwError(() => new Error('Erroooooo'))
)
)
)
.toPromise()
.then((d) => console.log(d)) // display indefined
.catch(() => console.log('ERRRRRROOOOOOR')); // thrown if d !=== 0
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/495979.html
標籤:javascript 打字稿 rxjs 可观察的 FP-TS
上一篇:從陣列中獲取資訊并將其加載到表中
下一篇:JavaScript物件陣列新值
