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?