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

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 
主站蜘蛛池模板: 陇西县| 东阿县| 高州市| 临潭县| 酉阳| 滦平县| 敖汉旗| 永平县| 凯里市| 东莞市| 郓城县| 秦皇岛市| 旬邑县| 无为县| 赫章县| 建德市| 灌南县| 乌兰县| 平阴县| 府谷县| 台北县| 太仓市| 鄢陵县| 昂仁县| 延吉市| 洱源县| 黄龙县| 宁陕县| 灯塔市| 延长县| 子洲县| 韶关市| 恩平市| 新泰市| 鹰潭市| 铁岭市| 开远市| 汤阴县| 海门市| 美姑县| 祁阳县|