Get state of Angular deferred?

Evan Hobbs picture Evan Hobbs · Jun 7, 2014 · Viewed 41.1k times · Source

With jQuery deferreds I'm used to be able to check the current state like this:

var defer = $.Deferred();
defer.state();  //Returns the state of the deferred, eg 'resolved'

Is there a way to do the same for Angular deferreds? (or even better promises)

Answer

Benjamin Gruenbaum picture Benjamin Gruenbaum · Jun 7, 2014

Update:

Due to refactoring of $q this is now possible although not documented:

promise.$$state.status === 0 // pending
promise.$$state.status === 1 // resolved
promise.$$state.status === 2 // rejected

Original:

Unlike most promise libraries (Bluebird,Q, when, RSVP etc), $q does not expose a synchronous inspection API.

There is no way to achieve this from the outside.

You have to call .then on the promise and code in that handler will run when the promise fulfills.