1

By defining a timeout you can set a time limit to wait for a web request. 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? I am a newbie in the world of angular.js.

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);

}

No, I do not know if I was not clear with my question. But I refer to the property "timeout" of web requests. no set timeout..

yavg
  • 2,761
  • 7
  • 45
  • 115

1 Answers1

0

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')
         );
Janith Widarshana
  • 3,213
  • 9
  • 51
  • 73
  • No, I do not know if I was not clear with my question. But I refer to the property "timeout" of web requests. – yavg May 11 '18 at 05:53
  • Do you want to set timeout after you got the response from async call ? – Janith Widarshana May 11 '18 at 05:56
  • 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. – yavg May 11 '18 at 05:57
  • 1
    This link will help you to get more understanding about pipeable operators https://github.com/ReactiveX/rxjs/blob/master/doc/pipeable-operators.md – Janith Widarshana May 11 '18 at 06:11
  • Thank you. I will try it when I can. should I import any additional libraries to those I have in my example? – yavg May 11 '18 at 13:40