Sabrina B.
Sabrina B.

Reputation: 376

Angular 7: How to increase the time of an angular request

I need to increase the request time of the angular app because using slow internet connections timeout happens.

I tried the code below and had an error.

this.http.post(url, body, { headers: headers })
            .timeout(100, this.handleTimeout)
            .map(response =>{
                return response;
            })
            .catch(this.handleErrors);

Property 'timeout' does not exist on type 'Observable'.ts(2339)

Not success using interceptor too

@Injectable()
export class AngularInterceptor implements HttpInterceptor {
  intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
    return next.handle(req).timeout(5000);
}

Property 'timeout' does not exist on type 'Observable>'.ts(2339)

Thanks

Upvotes: 0

Views: 2180

Answers (2)

Sabrina B.
Sabrina B.

Reputation: 376

The final solutions that works for me:

import { timeout} from 'rxjs/operators';

return this.http.get(`${url}`).pipe(
            timeout(1000)
        );

Thanks to all for the help.

Upvotes: 2

Piyush Saxena
Piyush Saxena

Reputation: 127

With Rxjs 6, you will have to use a .pipe and then use an operator like .timeout So your implementation should look like:

import { 
  timeout,
  map,
  catch
} from 'rxjs/operators';


this.http.post(url, body, { headers: headers })
            .pipe(
             timeout(100, this.handleTimeout),
             map(response =>{
                return response;
             }),
            catch(this.handleErrors);

Upvotes: 1

Related Questions