0

Using angular $q, i'm questioning myself if i should use $$state private properties to inspect the state of a promise (to check if it's pending, or completed).

Assuming a situation like this:

var promise = undefined;
$scope.click = function(){
   if(promise != null && promise.$$state.status === 0)
      return;

   promise = doAsyncAnimation().then(function(){
       console.log('hey, i'm done!');
   });
}

It is considered a bad practise? It would do exactly what i need, and i don't like use a separate boolean variable to do job. How much eligible would it be?

illeb
  • 2,942
  • 1
  • 21
  • 35
  • http://stackoverflow.com/questions/24091513/get-state-of-angular-deferred – mindparse Aug 19 '16 at 15:12
  • The question it's not about *how* to do it, but if it is considered a good practise. – illeb Aug 19 '16 at 15:16
  • The link is actually about it is in gray area. Some promise implementations offer promise inspection through their API (including Q, which $q was inspired with). Some don't. Some don't, but it still can be done. $q belongs to the latter. There are some cases where promise inspection is ok, but the question isn't one of them. – Estus Flask Aug 19 '16 at 15:26

1 Answers1

2

$$ name prefix designates a private property/service which is used internally and may be changed without notice.

From the manual:

Angular Prefixes $ and $$: To prevent accidental name collisions with your code, Angular prefixes names of public objects with $ and names of private objects with $$. Please do not use the $ or $$ prefix in your code

It is unlikely that $q will introduce breaking changes towards $$state. However, its usage indicates that promises are not used properly.

In this case it is simply

$scope.click = function(){
   if (promise)
      return;

   promise = doAsyncAnimation().then(function(){
       console.log('hey, i\'m done!');
   })
   .finally(function () {
       promise = null;
   });
}
Estus Flask
  • 206,104
  • 70
  • 425
  • 565