如何将retryWhen与返回布尔值的函数一起使用? [英] How to use retryWhen with a function that returns a Boolean?

查看:45
本文介绍了如何将retryWhen与返回布尔值的函数一起使用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的代码:

  this._http.post(this._url_get + extension,'',{headers:headers}).map(res => res ['_ body']).retryWhen(errors => {return responseErrorProcess(errors)}) 

现在我需要捕获异常并将其传递给我的 responseErrorProcess(),如果需要重试,该异常将返回 true

我无法弄清楚如何从错误中检索异常,这就是它的样子:

  Subject_isScalar:falseclosed:falsehasError:falseisStopped:falseobservers:Array [0] thrownError:null__proto__:可观察` 

它似乎不包含有关发生的异常的错误,而且我无法弄清楚应该返回什么才能真正重试.

解决方案

retryWhen 应该返回一个Observable.一旦发出可观察到的信号,便进行重试:

  .retryWhen(errors =>//switchMap检索源错误errors.switchMap(sourceErr =>//将源发送给处理器responseErrorsProcess(sourceErr)?//如果结果为TRUE,则发出(将导致重试).否则,传递错误Observable.of(true):Observable.throw(sourceErr))) 

如果要在处理器返回 false 时完成而不是出错,请用 Observable.empty()替换 Observable.throw()

Here's my code:

this._http.post(this._url_get + extension, '', { headers: headers })
    .map(res => res['_body'])
    .retryWhen(errors => {return responseErrorProcess(errors)})

now I need to catch exceptions and pass them to my responseErrorProcess() which returns true if it needs to retry

I could not figure out how to retrieve the exceptions from errors, this is how it looks:

Subject_isScalar: falseclosed: falsehasError: falseisStopped: falseobservers: Array[0]thrownError: null__proto__: Observable`

It doesn't seem to contain errors about the exceptions that occurs, plus I couldn't figure out what should I return in order to actually retry or not.

解决方案

retryWhen should return an Observable. The retry occurs once that observable emits:

.retryWhen(errors => 
    //switchMap to retrieve the source error
    errors.switchMap(sourceErr => 
        //send source to processor
        responseErrorsProcess(sourceErr) ? 
        //if result is TRUE, emit (will cause retry). Else, pass on the error
        Observable.of(true): Observable.throw(sourceErr)
    )
)

If you want to complete instead of error when your processor returns false, replace Observable.throw() with Observable.empty()

这篇关于如何将retryWhen与返回布尔值的函数一起使用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆