1

I am following this solution to implement a timeout in a http call to a remote server.

This is the calling function; if the request returns null then the alert will be put into view:

this.service.getAll().subscribe(response => {
      console.log("home.page: calling servuce for data...data returned is : " + response);
      if (response != null ){
        this.tests = response;
        this.searching = false; //Turn spinner off
      }else{
        //http timeout after 2000ms
        this.alertCtrl.create({
          header: "Connection Timeout",
          message: "Check your internet connection!",
          buttons: [
            {
            text: "Ok"
            }
        ]

        }).then(alert => alert.present());

This is the http service; if the http call times out after 10s (1000ms) null will be returned to the caller (above):

import { timeout, catchError } from 'rxjs/operators';
import { of } from 'rxjs/observable/of';

    getAll(){
        //return this.http.get<[Student]>(this.url);
        console.log("student.service-59- GET ALL called..etunr null if times out after 10000ms");
        return this.http.get<[Test]>(this.url)
          .pipe(
            timeout(10000),
            catchError(e => {
              console.log("student service  GET ALL timed out, retunr null, error = ");
              return of(null);
              
            })
          )
    
      }

However when I test with the internet connection off the alert / prompt appears immediately and does not appear to wait 10seconds as defined in the timeout argument.

Any idea why this is the case?

3
  • Wrong angular tag. [angularjs] is for 1.x, [angular] is for 2+ Commented May 28, 2021 at 22:21
  • 1
    timeout() will call the catchError function if there is no response from server for 10s, but in your case since the internet connection is off you will get a failed status immediately, which is why you are seeing the alert without those 10s being elapsed Commented May 29, 2021 at 13:32
  • So is it the catchError is catching error thrown by no internet connection ? I came across a test server to simulate a slow connection on slowwly.robertmurray.com.uk but this website doesn’t seem to be active Commented May 29, 2021 at 14:51

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.