17

Is it possible to make a timeout of 3 seconds in a post request ? How ?

My code for the moment

this.http.post('myUrl', 
    MyData, {headers: Myheaders})
        .map(res => res.json())
        .subscribe(
            data => this.ret = data,
            error => console.debug('ERROR', error),
            () => console.log('END')
        );
Dov Benyomin Sohacheski
  • 7,133
  • 7
  • 38
  • 64
Raph
  • 521
  • 3
  • 7
  • 15
  • the http request gives ERR_EMPTY_RESPONSE after 4 minutes from sending the request. Is there any way to increase this time?. I tried the .timeout() method. But it still gives error after 4 minutes – Anandhu Ajayakumar Apr 10 '18 at 11:45

3 Answers3

35

You can use the timeout operator like this:

this.http.post('myUrl', 
        MyData, {headers: Myheaders})
         .timeout(3000, new Error('timeout exceeded'))
         .map(res => res.json())
         .subscribe(
           data => this.ret = data,
           error => console.debug('ERROR', error),
           () => console.log('END')
         );
Pankaj Parkar
  • 134,766
  • 23
  • 234
  • 299
Thierry Templier
  • 198,364
  • 44
  • 396
  • 360
  • Thanks for replying, but it doesn't work for me, it is always execute "return new Error('delay exceeded')" – Raph Mar 03 '16 at 18:14
  • Oh sorry! There was a typo in my answer. I updated it. If you could make a new try... – Thierry Templier Mar 03 '16 at 18:22
  • @ThierryTemplier instead of returning `new Error` we should throw an error `throw new Error` so that you can see that error in console with actual `red color` – Pankaj Parkar Mar 03 '16 at 20:31
  • @PankajParkar Nope, instead of reading it with console.debug we should throw it. Anyway it's passing the error as an argument to the function. if you throw there it's a syntax error, and would always throw, not only on timeouts. – SparK Dec 10 '16 at 16:12
  • what kind of timeout of above? Connection timeout? Socket timeout? Looks like it's only an observable timeout. What I'm confused is that if server properly handled the response and returned to client more than 2000 millseconds, what's happened then? – fifth Jul 21 '17 at 07:33
23

You might need to import timeout like so

import 'rxjs/add/operator/timeout'

I couldn't get it to work without that on rxjs 5.0.1 and angular 2.3.1

Edit 20 April 2018

please refrain from importing operators that way, in rxjs +5.5 you can import operators like this one with

import { timeout } from 'rxjs/operators/timeout';
// Use it like so
stream$.pipe(timeout(3000))

Please checkout this document which explains pipeable operators in great depth.

realappie
  • 4,656
  • 2
  • 29
  • 38
13

I modified the answer of Thierry to get it working with the latest release. It is necessary to remove the second parameter of the timeout function. According to a discussion regarding an angular-cli issue, the timeout function always throws a TimeoutError now.

this.http.post('myUrl', 
    MyData, {headers: Myheaders})
     .timeout(3000)
     .map(res => res.json())
     .subscribe(
       data => this.ret = data,
       error => console.debug('ERROR', error),
       () => console.log('END')
     );
Peter
  • 211
  • 2
  • 8