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

Davide Dell'Olio
  • 83
  • 1
  • 2
  • 6
  • 4
    Possible duplicate of [How to timeout angular2 http request](https://stackoverflow.com/questions/36719116/how-to-timeout-angular2-http-request) – JB Nizet Sep 30 '17 at 14:00
  • I asked for angular 4. Thank you – Davide Dell'Olio Sep 30 '17 at 18:28
  • 2
    Angular 4, just as Angular 2, relies on RxJS. Why don't you at least read and try the answer? – JB Nizet Sep 30 '17 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'. – Davide Dell'Olio Sep 30 '17 at 18:36

2 Answers2

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

MatPag
  • 41,742
  • 14
  • 105
  • 114
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());
} 
bgraham
  • 1,939
  • 1
  • 10
  • 17
  • 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'. – Davide Dell'Olio Oct 01 '17 at 12:30
  • try adding import "rxjs/add/observable‌​/timeout"; to your imports. Rxjs needs to import most functions specifically. – bgraham Oct 01 '17 at 15:22
  • 1
    Yes, The solution is import { Observable } from 'rxjs/Rx'; Thank you – Davide Dell'Olio Oct 02 '17 at 11:03