Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Check Whether an Angular $q promise Is Resolved

I understand that typically one would just attach continuation code with a then() call and chain behaviour when using promises.

However, I want to kick off a promise-wrapped asynchronous call and then separately kick off a 3-second $timeout() so I can take a UI action, ONLY IF the original promise has not yet completed. (I anticipate that this would only happen on slow connections, mobile devices on 3G, etc.)

Given a promise, can I check whether it's complete or not without blocking or waiting?

like image 730
blaster Avatar asked Jul 26 '13 16:07

blaster


People also ask

What is resolve in Promise Angular?

Promise. reject() : It returns a new Promise object that is rejected with the given reason. Promise. resolve() : It returns a new Promise object that is resolved with the given value.

What does $q do in AngularJS?

$q is an angular defined service. It's the same as new Promise(). But $q takes things to the next level by enhancing additional feature that developers can use to perform complex tasks more simply. resolve(value) – resolves the derived promise with the value.

Can Promise be Cancelled in Angular?

You can wait for promises to finish, but you can't cancel them. You could disable the following next button until they have all completed.

What is a promise in angular?

Angular – Promise Explained with Code Example. Promise, in Javascript, is a concept which allows the callee function to send back a promise (sort of assurance) to the caller function that it would, for sure, send back a resolution, be it a success or a failure at a little later point of time.

What does it mean when a promise is resolved or rejected?

It means that the promise has been resolved and now has its resolved value (using the internal resolve function). The operation represented by the promise has been completed successfully. Rejected means that the promise has been rejected and now has its rejected reason (using the internal reject function).

Should I use $Q() for non-promise stuff?

Golden rule: use $q for non-promise stuff, that’s it! Well, only create your Promises in this case, however you can use other methods such as $q.all() and $q.race() alongside other promises. $q.defer() Using $q.defer() is just another flavour, and the original implementation, of $q() as a Promise constructor.

What does it mean when a promise has been fulfilled?

Fulfilled is a state of a Promise. It means that the promise has been resolved and now has its resolved value (using the internal resolve function). The operation represented by the promise has been completed successfully.


2 Answers

I guess this was added in a recent version of Angular but there seems to be now an $$state object on the promise:

 var deferred = $q.defer();  console.log(deferred.promise.$$state.status); // 0  deferred.resolve();  console.log(deferred.promise.$$state.status); //1  

As noted in the comments this is not recommended as it might break when upgrading your Angular version.

like image 130
parliament Avatar answered Sep 21 '22 10:09

parliament


I think your best option as is, (without modifying the Angular source and submitting a pull request) is to keep a local flag for if the promise has been resolved. Reset it every time you setup the promise you're interested in and mark it as complete in the then() for the original promise. In the $timeout then() check the flag to know if the original promise has resolved yet or not.

Something like this:

var promiseCompleted = false; promise.then(function(){promiseCompleted=true;}) $timeout(...).then(function(){if(!promiseCompleted)doStuff()}) 

Kris Kowal's implementation includes other methods for checking the state of the promise but it appears Angular's implementation of $q unfortunately doesn't include these.

like image 39
shaunhusain Avatar answered Sep 20 '22 10:09

shaunhusain