【问题标题】:Angular 6+ / Retry Service Call on Error Callback?Angular 6+ / 错误回调时重试服务调用?
【发布时间】:2019-03-30 14:07:36
【问题描述】:

我有一个可能引发错误的服务调用的实现,如果它出错,是否可以重新运行服务调用?

captureMultiScreen(elem: Alert, overwrite: boolean, success?: Function, failed?: Function) {
this.spinnerValue = true;
const captureModel: Capture = {
  member_id: elem.member_id,
  gateway: elem.gateway,
  account: elem.account,
  alert_id: elem.alert_id,
  transaction_id: elem.transaction_id,
  overwrite: overwrite
};

this.captureService.CaptureScreenService(captureModel).subscribe(result => {
  this.capServiceResponse = result;
},
(error) => {
  // Retry "CaptureScreenService" here
}

如果服务调用到达错误回调,有没有办法再次运行服务调用?

【问题讨论】:

  • @sanfalero 我明白了,这个 retry() 方法应该在我发出 http 请求的同一个函数中调用?
  • 你可以在任何 observable 上使用它。也许像 this.captureService.CaptureScreenService(captureModel).pipe(retry(3)).subscribe(...
  • @sanfalero 就像一个魅力!谢谢;如果您想将此作为答案,我会将其选为已接受

标签: angular typescript xmlhttprequest rxjs observable


【解决方案1】:

您可以使用retry function。在您的代码中,它将类似于:

this.captureService.CaptureScreenService(captureModel)
  .pipe(retry(3)).subscribe(...)

【讨论】:

    猜你喜欢
    • 2018-11-18
    • 2019-05-26
    • 1970-01-01
    • 2019-03-30
    • 1970-01-01
    • 2018-05-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多