3

I have seen in angularjs 4 official page (https://angular.io/guide/http) to set http call timeout but I did not find any reference. Has anyone tried to set it up?

Thank you

4
  • 4
    Possible duplicate of How to timeout angular2 http request Commented Sep 30, 2017 at 14:00
  • I asked for angular 4. Thank you Commented Sep 30, 2017 at 18:28
  • 2
    Angular 4, just as Angular 2, relies on RxJS. Why don't you at least read and try the answer? Commented Sep 30, 2017 at 18:30
  • var response = this.http.get(API.root + path, options).timeout(3000, new Error('timeout exceeded')).map((res : Response)=> this.checkResponse(res)); I have this error: Property 'timeout' does not exist on type 'Observable<Response>'. Commented Sep 30, 2017 at 18:36

2 Answers 2

8

If you are using RxJS version 6 and above the current syntax is this:

import { timeout } from 'rxjs/operators';
...
getUsers() {
   return this.http.post(API_URL, {headers: Myheaders})
      .pipe(
          timeout(5000) //5 seconds
      );
} 

Reference: https://rxjs-dev.firebaseapp.com/api/operators/timeout

Sign up to request clarification or add additional context in comments.

Comments

4

There is a timeout operator:

getUsers() {
   return this.http.post(this.baseUrl + "users", {headers: Myheaders})
         .timeout(3000, new Error('timeout exceeded'))
         .map(res => res.json());
} 

3 Comments

var response = this.http.get(API.root + path, options).timeout(3000, new Error('timeout exceeded')).map((res : Response)=> this.checkResponse(res)); I have this error: Property 'timeout' does not exist on type 'Observable<Response>'.
try adding import "rxjs/add/observable‌​/timeout"; to your imports. Rxjs needs to import most functions specifically.
Yes, The solution is import { Observable } from 'rxjs/Rx'; Thank you

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.