RxJS: Observable.never() keeps like subscription

前端 未结 2 2024
天命终不由人
天命终不由人 2021-01-29 13:04

I\'m using rxjs 5.5.6.

I\'ve created this code in order to show the behavior:

Observable.of(1, 2)
    .do(a => {
        console.log(a);
        let d         


        
2条回答
  •  不要未来只要你来
    2021-01-29 13:33

    Yes all the explanation by @frido is correct. With that answer I would like to add:

    If you want to capture any error occuring in any particular Observable itself(say an HTTP request) then you need to handle it in that particular erroring Observable.

      let correct = Observable.of("correct")
      let inCorrect = Observable.throw('inCorect')
    
      let obs = [inCorrect, correct];
      let handledObs = obs.map(eachObs => {
        return eachObs.catch((e) => {
          console.log("Individual handler, Good Catch!");
          return Observable.of("I am tampered");
        })
      })
    
      forkJoin(...handledObs)
      .do(a => {
          console.log(a);
      })
      .catch(() => {
          console.log("error catched");
          return Observable.never();
      })
      .subscribe(data => {
        console.log(`data`, data)
      },(e) => {
          console.log(`error`, e)
      });
    
    }
    

    See an example here: https://stackblitz.com/edit/angular-7mmhn7?file=src/app/app.component.ts


    EDIT

    However when I look at your code, it looks to me that you are trying to log something and the returned data may not have a length property, even in that case you want to proceed with the stream. If this is true then you can add a simple try catch under do()

      from([1, 2])
      .do(a => {
          try {
            console.log(a);
            let d:string = null;
            let r = d.length;
          } catch(e) {
            console.log(`catched under do`, e)
          }
      })
      .catch(() => {
          console.log("error catched");
          return Observable.of('catched');
      })
      .subscribe(data => {
        console.log(`data`, data)
      },(e) => {
          console.log(`error`, e)
      });
    

    Here is an example: https://stackblitz.com/edit/angular-bcrava?file=src/app/app.component.ts

提交回复
热议问题