Che*_*rra 41 javascript rxjs reactivex
我有一个要解析的项目列表,但其中一个项目的解析可能会失败.
什么是"Rx-Way"来捕获错误但继续执行序列
代码示例:
var observable = Rx.Observable.from([0,1,2,3,4,5])
.map(
function(value){
if(value == 3){
throw new Error("Value cannot be 3");
}
return value;
});
observable.subscribe(
function(value){
console.log("onNext " + value);
},
function(error){
console.log("Error: " + error.message);
},
function(){
console.log("Completed!");
});Run Code Online (Sandbox Code Playgroud)
<script src="https://cdnjs.cloudflare.com/ajax/libs/rxjs/4.0.6/rx.all.js"></script>Run Code Online (Sandbox Code Playgroud)
我想以非Rx方式做什么:
var items = [0,1,2,3,4,5];
for (var item in items){
try{
if(item == 3){
throw new Error("Value cannot be 3");
}
console.log(item);
}catch(error){
console.log("Error: " + error.message);
}
}Run Code Online (Sandbox Code Playgroud)
提前致谢
pau*_*els 36
我建议你使用flatMap(现在mergeMap在rxjs版本5中),如果你不关心它们,它会让你崩溃错误.实际上,您将创建一个内部Observable,如果发生错误,可以吞下它.这种方法的优点是可以将运算符链接在一起,如果管道中的任何地方发生错误,它将自动转发到catch块.
const {from, iif, throwError, of, EMPTY} = rxjs;
const {map, flatMap, catchError} = rxjs.operators;
// A helper method to let us create arbitrary operators
const {pipe} = rxjs;
// Create an operator that will catch and squash errors
// This returns a function of the shape of Observable<T> => Observable<R>
const mapAndContinueOnError = pipe(
//This will get skipped if upstream throws an error
map(v => v * 2),
catchError(err => {
console.log("Caught Error, continuing")
//Return an empty Observable which gets collapsed in the output
return EMPTY;
})
)
const observable = from([0, 1, 2, 3, 4, 5]).pipe(
flatMap((value) =>
iif(() => value != 3,
of(value),
throwError(new Error("Value cannot be 3"))
).pipe(mapAndContinueOnError)
)
);
observable.subscribe(
(value) => console.log("onNext " + value), (error) => console.log("Error: " + error.message), () => console.log("Completed!")
);Run Code Online (Sandbox Code Playgroud)
<script src="https://unpkg.com/rxjs/bundles/rxjs.umd.min.js"></script>Run Code Online (Sandbox Code Playgroud)
iam*_*rns 26
您需要切换到新的一次性流,如果在其中发生错误,将安全地处理,并保持原始流的活动:
Rx.Observable.from([0,1,2,3,4,5])
.switchMap(value => {
// This is the disposable stream!
// Errors can safely occur in here without killing the original stream
return Rx.Observable.of(value)
.map(value => {
if (value === 3) {
throw new Error('Value cannot be 3');
}
return value;
})
.catch(error => {
// You can do some fancy stuff here with errors if you like
// Below we are just returning the error object to the outer stream
return Rx.Observable.of(error);
});
})
.map(value => {
if (value instanceof Error) {
// Maybe do some error handling here
return `Error: ${value.message}`;
}
return value;
})
.subscribe(
(x => console.log('Success', x)),
(x => console.log('Error', x)),
(() => console.log('Complete'))
);Run Code Online (Sandbox Code Playgroud)
<script src="https://cdnjs.cloudflare.com/ajax/libs/rxjs/5.4.1/Rx.min.js"></script>Run Code Online (Sandbox Code Playgroud)
有关此技术的更多信息,请参阅此博客文章:寻找肉丸:发生错误时继续使用RxJS Streams
Ved*_*ran 12
为了使您endlessObservable$从垂死的,你可以把你failingObservable$在高阶绘图操作(例如switchMap,concatMap,exhaustMap...),并终止与内流吞下的错误empty()观察到没有返回值。
使用 RxJS 6:
endlessObservable$
.pipe(
switchMap(() => failingObservable$
.pipe(
catchError((err) => {
console.error(err);
return EMPTY;
})
)
)
);
Run Code Online (Sandbox Code Playgroud)
Toa*_*yen -5
实际上,您可以在映射函数中使用try/catch来处理错误。这是代码片段
var source = Rx.Observable.from([0, 1, 2, 3, 4, 5])
.map(
function(value) {
try {
if (value === 3) {
throw new Error("Value cannot be 3");
}
return value;
} catch (error) {
console.log('I caught an error');
return undefined;
}
})
.filter(function(x) {
return x !== undefined; });
source.subscribe(
function(value) {
console.log("onNext " + value);
},
function(error) {
console.log("Error: " + error.message);
},
function() {
console.log("Completed!");
});Run Code Online (Sandbox Code Playgroud)
<script src="https://cdnjs.cloudflare.com/ajax/libs/rxjs/4.0.6/rx.all.js"></script>Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
41549 次 |
| 最近记录: |