zoukankan      html  css  js  c++  java
  • [RxJS] Error handling operator: catch

    Most of the common RxJS operators are about transformation, combination or filtering, but this lesson is about a new category, error handling operators, and its most important operator: catch().

    Basic catch( err => Observable):

    var foo = Rx.Observable.interval(500)
      .zip(Rx.Observable.of('a','b','c','d',2), (x,y)=>y);
    
    var bar = foo.map(x => x.toUpperCase());
    
    /*
    --a--b--c--d--2|     (foo)
    map(toUpperCase)
    --A--B--C--D--#      (bar)
    catch(# => ###|)
    --A--B--C--D--###|
    */
    
    var result = bar.catch(error => Rx.Observable.of('###'));
    
    result.subscribe(
      function (x) { console.log('next ' + x); },
      function (err) { console.log('error ' + err); },
      function () { console.log('done'); },
    );

    Retry with catch( (error, outputObs) => Observable):

    var foo = Rx.Observable.interval(500)
      .map( () => Math.random());
    
    var bar = foo.map(x => {
      if(x < 0.5){
        return x;
      }else{
        throw "Error, too large, try again"
      }
    });
    
    var result = bar
      .catch(
         (error, outputObs) => {
            return outputObs;
         }
      );
    
    result.subscribe(
      function (x) { console.log('next ' + x); },
      function (err) { console.log('error ' + err); },
      function () { console.log('done'); },
    );

    Code check whether the x is large than 0.5, if is then throw error, if not, then continue. 

    bar$ catch the error and use 'outputObs' to repeat the action, so evenytime, it hits the error, it will restart.

  • 相关阅读:
    SSM框架搭建
    UML——类图
    javascript中的闭包(Closure)的学习
    JS和jQuery中ul li遍历获取对应的下角标
    jquery中防止冒泡事件
    chouTi
    DjangoForm 之创建FORM模板进行验证
    django学习日记-cookie
    同步锁
    进程线程
  • 原文地址:https://www.cnblogs.com/Answer1215/p/5547170.html
Copyright © 2011-2022 走看看