How to Check Whether an Angular $q promise Is Resolved How to Check Whether an Angular $q promise Is Resolved angularjs angularjs

How to Check Whether an Angular $q promise Is Resolved


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.


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.


It doesn't seem to be possible, as @shaunhusain already mentioned. But maybe it's not necessary:

// shows stuff from 3s ahead to promise completetion, // or does and undoes it in one step if promise completes before$q.all(promise, $timeout(doStuff, 3000)).then(undoStuff);

or maybe better:

var tooSlow = $timeout(doStuff, 3000);promise.always(tooSlow.cancel);