0

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?

2

1 Answer 1

0

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.

Sign up to request clarification or add additional context in comments.

Comments

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.