6

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 + 'Search/Everything', params, {withCredentials: true, headers: this.headers})
                .timeout(5000, this._feedbackService.error("Custom error message"))
                .map((response: Response) => response.json().data);

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?

1 Answer 1

9

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);
Sign up to request clarification or add additional context in comments.

8 Comments

Sorry. I answered this and then realised I'd misread the question. I've updated the answer.
What do I need to import besides rxjs/add/operator/timeoutWith to make this work? (the "Rx." part that is).
You will need import "rxjs/add/observable/defer" and import "rxjs/add/observable/throw", too.
I still get a red squiggly line under "Rx.".
Sorry. Updated the code to use the minimal Observable together with the appropriate import statements.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.