Does anyone know how to implement a timeout on an HTTP Request in an Ionic 2 ?
I can’t find any examples yet of any Angular 2 Http timeouts.
Does anyone know how to implement a timeout on an HTTP Request in an Ionic 2 ?
I can’t find any examples yet of any Angular 2 Http timeouts.
There’s a chainable function for observables, heres one with 1 sec timeout:
this.http.get(url)
.timeout(1000)
.map....
Thanks. How do I run some code when the timeout occurs?
It still gets to the subscribe
method, a timeout is an error so it should get to the error part:
this.http.get(url)
.timeout(1000)
.map(...)
.subcribe((success) => {...}, (err) => { // Deal here with timeout error. });
Don’t take my word for granted, check the docs, i may be wrong if it goes in the success or the error function.
Oh I see. Thank you - I will that that out.
@luchillo17 thank you very much - that worked.
It took me almost a day to figure this out, as angular 2 uses ES6 it’s like learning a whole new language.
In rxjs/Observable.d.ts (5.0.0-beta.6, the current ionic 2 default) the .timeout(1000) function is not defined. And when I try to force it I get the error: this.http.post(...).timeout is not a function error send
I do need to use this (no response at all currently creates a deadlock) but I have no idea how to fix this issue.
In latest versions of Angular 2 the operators need to be imported where you get to need them in the observable, most popular ones are:
import {Http, Headers} from '@angular/http';
import 'rxjs/add/operator/retry';
import 'rxjs/add/operator/timeout';
import 'rxjs/add/operator/delay';
import 'rxjs/add/operator/map';
Thank you! I was having problems trying to transpile because timeout was not imported.
What is default timeout ?
What do you mean by default timeout? if you mean the rxjs timeout operator, it let’s you timeout an observable and return an error in the specified time.
Say i want to make an http request and make sure it doesn’t run forever, i put a 10s timeout so if it hasn’t finished in that time the observable emits an error.
It means no default value.
Default value is 1 minute.
Is it possible to set it to 3 minutes?
Yes.
Use like this:
this.http.get(url).timeout(180000).
I’ve tried this first, but it didn’t work.
The default timeout or the timeout of the web view fires first.
And I haven’t found any solution yet setting the timeout longer than the default.
@nsvivek121
It’s working now like you told.
Even though I didn’t import timeout
, npm run serve
didn’t throw an error.
The above solution works, but only for the angular http requests.
It does not work with the ionic native http plugin.
Does anyone know how to do the same with the ionic native http plugin.
I searched a lot but found no way to do the same in the ionic native HTTP plugin.
Sorry man, haven’t tested the native implementation, however it returns a promise, unless the promise itself resolves or rejects when timeout, it cannot be timed out.
Given the docs on native/http, from it’s api i get that it doesn’t support timeout operations.