Reputation: 2559
I am using AngularJS and Typescript. In my controller constructor, I make a call to a function that gets data and returns a promise. The promise is stored in the variable x
.
x.then((data) => {
//displays data
}, (err) => {
//sends error message
});
Right now if the promise never resolves, it displays nothing. I want to use $timeout
so that if the resolution of x
takes too long it will display another message. I don't know how I can do it for promises. Calling this.$timeout(3000,true).then(() => {});
just delays the contents and I can delay a function. How can I abort if the promise doesn't resolve?
Upvotes: 0
Views: 193
Reputation: 48968
AngularJS v1.6 introduces the $q.race method which can be used to abort a promise:
var timeoutPromise = $timeout(null, delay).then(() => {throw "Timeout Abort" });
var timedPromise = $q.race(xPromise, timeoutPromise);
In this example, timedPromise
will either resolve (fulfilled or rejected) with the results of xPromise
or be rejected with the reason "Timeout Abort" whichever comes first.
Upvotes: 0