Angular 2 rxjs 超时回调 [英] Angular 2 rxjs timeout callback
本文介绍了Angular 2 rxjs 超时回调的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想在 HTTP 调用超时事件发生时向用户提供反馈.
I want to provide feedback to the user if a timeout event on a HTTP call happens.
我试过了:
return this._http
.post(apiUrl + 'Search/Everything', params, {withCredentials: true, headers: this.headers})
.timeout(5000, this._feedbackService.error("Custom error message"))
.map((response: Response) => response.json().data);
但是,一旦发出 HTTP 调用,就会触发反馈服务.
But that fires the feedback service as soon as the HTTP call is made.
如何在超时开始时启动服务?
How do I fire the service when the timeout kicks in?
推荐答案
假设 _feedbackService.error
返回一个 Error
,您应该能够使用timeoutWith
和 defer
运算符:
Assuming _feedbackService.error
returns an Error
, you should be able to do what you need with the timeoutWith
and defer
operators:
import "rxjs/add/observable/defer";
import "rxjs/add/observable/throw";
import "rxjs/add/operator/timeoutWith";
import { Observable } from "rxjs/Observable";
return this._http
.post(apiUrl + 'Search/Everything', params, {withCredentials: true, headers: this.headers})
.timeoutWith(5000, Observable.defer(() => Observable.throw(this._feedbackService.error("Custom error message"))))
.map((response: Response) => response.json().data);
这篇关于Angular 2 rxjs 超时回调的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文