Angular 2 rxjs timeout callback

后端 未结 1 1501
天命终不由人
天命终不由人 2020-12-20 21:50

I want to provide feedback to the user if a timeout event on a HTTP call happens.

I tried this:

return this._http
                .post(apiUrl + \'S         


        
相关标签:
1条回答
  • 2020-12-20 22:11

    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);
    
    0 讨论(0)
提交回复
热议问题