How to Check Whether an Angular $q promise Is Resolved

blaster picture blaster · Jul 26, 2013 · Viewed 56.2k times · Source

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?

Answer

parliament picture parliament · Apr 11, 2015

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.