Here is just regular request looking like that:
this.people = http.get('http://localhost:3000/users') .map(response => response.json());
Is there any way to delay/timeout that?
In complement to the other answers, just beware that if you use the proxy config on the dev machine, the proxy's default timeout is 120 seconds (2 minutes).
The HyperText Transfer Protocol (HTTP) 408 Request Timeout response status code means that the server would like to shut down this unused connection. It is sent on an idle connection by some servers, even without any previous request by the client.
You can leverage the timeout
operator of observables, as described below:
return this.http.get('http://api.geonames.org/postalCodeSearchJSON', { search: params }) .retryWhen(error => error.delay(500)) .timeout(2000, new Error('delay exceeded')) // <------ .map(res => res.json().postalCodes);
The return value of http.get()
is an observable, not the response. You can use it like:
getPeople() { return http.get('http://localhost:3000/users') .timeout(2000) .map(response => response.json()); } } foo() { this.subscription = getPeople.subscribe(data => this.people = data) } // to cancel manually cancel() { this.subscription.unsubscribe(); }
See also https://github.com/Reactive-Extensions/RxJS/blob/master/doc/api/core/operators/timeout.md
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With