Luxor001 Luxor001 - 3 months ago 21
AngularJS Question

Should i use private properties $$state in $q for my needs?

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?

Answer

$$ 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;
   });
}