官术网_书友最值得收藏!

Catch

The use of onError gives us a much better experience overall, but it isn't very flexible.

Let's imagine a different scenario, where we have an observable retrieving data from the network. What if, when this observer fails, we would like to present the user with a cached value instead of an error message?

This is where the catch combinator comes in. It allows us to specify a function to be invoked when the observable throws an exception, much like OnError does.

Differently from OnError, however, catch has to return a new observable that will be the new source of items from the moment the exception was thrown:

(rx/subscribe (->> (exceptional-obs) 
                   (rx/catch Exception e 
                       (rx/return 10)) 
                   (rx/map inc)) 
              (fn [v] (prn-to-repl "result is " v))) 
 
;; "result is " 11 

In the previous example, we are essentially specifying that, whenever exceptional-obs throws, we should return the value 10. We are not limited to single values, however. In fact, we can use any observable we like as the new source:

(rx/subscribe (->> (exceptional-obs) 
                   (rx/catch Exception e 
                     (rx/seq->o (range 5))) 
                   (rx/map inc)) 
              (fn [v] (prn-to-repl "result is " v))) 
 
;; "result is " 1 
;; "result is " 2 
;; "result is " 3 
;; "result is " 4 
;; "result is " 5 
主站蜘蛛池模板: 双江| 隆德县| 滦平县| 大洼县| 东至县| 富源县| 满城县| 仲巴县| 军事| 尉氏县| 宣武区| 汾阳市| 如皋市| 诏安县| 和田县| 类乌齐县| 荣成市| 龙泉市| 沙洋县| 沾益县| 恩平市| 甘洛县| 贵阳市| 东兰县| 临沭县| 祁连县| 北海市| 南城县| 扎赉特旗| 鹰潭市| 康乐县| 彭水| 师宗县| 永和县| 梁山县| 六枝特区| 肥西县| 阿坝| 双桥区| 鄂尔多斯市| 武川县|