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')
        );
1
  • 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 Commented Apr 10, 2018 at 11:45

3 Answers 3

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

5 Comments

Thanks for replying, but it doesn't work for me, it is always execute "return new Error('delay exceeded')"
Oh sorry! There was a typo in my answer. I updated it. If you could make a new try...
@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
@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.
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?
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.

1 Comment

Thanks for the answer, it needs to be imported for angular 5+
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')
     );

1 Comment

Confirmed that an error can't be passed as the second argument in Angular 4

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.