当前位置:网站首页>After an error occurs in the source observable, it will be directly entered into the catchrror operator instead of the sequence in the pipe before entering the catchrror

After an error occurs in the source observable, it will be directly entered into the catchrror operator instead of the sequence in the pipe before entering the catchrror

2022-06-10 11:15:00 zsy_ one thousand nine hundred and ninety-one

catchError Use notes

I always thought source observable Even after an error , I will also follow pipe Inside operator After the sequential processing of catchError, It's not , Is to skip the previous operator Handle , Directly be catchError operator catch live . Examples are as follows :

const example$ =  new Subject<number>();
//  When source observable  An error occurred ,pipe  Inside operator  They're not leaving , Go directly catachError
example$.pipe(
   map(x => {
    
     console.log(x);  //  When source observable  After an error , This sentence will not execute 
     return x + 1;
   }),
   catchError(error => {
    
       console.log(error);
       throw 'excute ' + error + ' failed';
   })
 ).subscribe(
     next => {
    
         console.log('value=', next); 
     },
     error => {
    
         console.log('had error: ', error); 
     }
 );

 example$.next(1);
 example$.error(2);

The results are as follows :

map 1
value= 2
catchError 2
had error:  excute 2 failed
原网站

版权声明
本文为[zsy_ one thousand nine hundred and ninety-one]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/161/202206101108200849.html