-2

Timeout is the time limit that a web request can not pass. For example, if I define a timeout of 3 seconds, the web request while requesting the data, is canceled if it exceeds 3 seconds. I would like my web service not to exceed 3 seconds.

How can I do it?

import { Injectable } from '@angular/core';
import { HttpClient, HttpHeaders, HttpParams} from '@angular/common/http';
import { map } from 'rxjs/operators';
import { Observable } from 'rxjs/Observable';
import { GLOBAL } from '../app.constantes';

@Injectable()
export class AppService{
public url: string;

constructor(
    public _http: HttpClient

){}
getAll(url,method,param): Observable<any>{
    let config={};
    config["timeout"]=3000;
    config["data"]=param ? param: {}; //in case of POST  this is the "data" property, with GET is "params" I believe..
    return this._http.post(url,config);

}
Alexander Abakumov
  • 13,617
  • 16
  • 88
  • 129
yavg
  • 2,761
  • 7
  • 45
  • 115

1 Answers1

1

You can use timeout operator of rxjs :

return this._http.post(url,config).timeout(3000, new Error('request timeout'));

then in subscribe part you can specify action for successful result or error:

getAll(url,method,param).subscribe(
            data => successFunction(data),
            error => errorFunction(error)
        )
Masso
  • 56
  • 4