在下面的代码片段中,我使用 httpClient
从端点获取用户列表,
在一个完美的世界中,应用程序发送一个 http 请求并获取用户列表,
但是当服务器返回 4xx
或 5xx
错误时,应用程序会在 10 毫秒后重试并不断向服务器发送数千个 http 请求。
this.http.get<User[]>(SERVER_API_URL + '/api/not_found')
.subscribe(
value => console.log('subscribe.next'),
err => console.log('subscribe.error'),
() => console.log('subscribe.done')
);
如何阻止应用程序重试。
我尝试将 Observable
转换为 Promise
但我得到了相同的结果。
- "@angular/core": "6.0.0"
- "@angular/http": "6.0.5"
- "rxjs": "6.2.1"
最佳答案
重试方法可以自动重新订阅失败的 Observable 指定次数。重新订阅 HttpClient 方法调用的结果具有重新发出 HTTP 请求的效果。
this.http.get<User[]>(SERVER_API_URL + '/api/not_found')
.pipe(
retry(0), // retry a failed request up to 0 times
catchError(this.handleError) // then handle the error
).subscribe(...);
}
关于angular - 防止 Observable 在服务器关闭时重试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50897739/