2

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..

1
  • so you want your rest call to complete within 3 mins ? Commented May 11, 2018 at 6:06

1 Answer 1

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

5 Comments

No, I do not know if I was not clear with my question. But I refer to the property "timeout" of web requests.
Do you want to set timeout after you got the response from async call ?
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.
This link will help you to get more understanding about pipeable operators github.com/ReactiveX/rxjs/blob/master/doc/pipeable-operators.md
Thank you. I will try it when I can. should I import any additional libraries to those I have in my example?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.