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

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 
主站蜘蛛池模板: 桐乡市| 穆棱市| 搜索| 鄢陵县| 丰都县| 阳信县| 肃南| 潮州市| 友谊县| 大荔县| 呼和浩特市| 葵青区| 寿宁县| 浦县| 钟祥市| 奇台县| 灵寿县| 肃宁县| 阳新县| 五寨县| 木兰县| 改则县| 休宁县| 重庆市| 藁城市| 盐城市| 奉贤区| 沂源县| 东阳市| 中牟县| 辽宁省| 乌兰浩特市| 白城市| 哈尔滨市| 烟台市| 大同县| 贡嘎县| 万源市| 康平县| 美姑县| 石城县|