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)
Angular $q,promises and deferred. Promises are logics that are executed after any function is completed. In AngularJS, promises are provided by the in-built $q service. It executes asynchronous functions by registering them with the promise object. Deferred helps to control how and when those promise logics will execute.
Deferred-Promises is a design pattern which helps you to deal with these callbacks. Before starting Deferred & Promises in AngularJS, Let’s understand callback and why we actually need Deferred-Promises in AngularJS or any other language.
The examples are all written in Angular, however the approaches are not specific to Angular. In this article we will learn about the different kind of state types within our application, and where that state might live. What is state? State is basically everything that will define the UI that our user will be using.
Deferred returns promise object. When Deferred completes, You call methods either resolve (), reject (), and notify () . It calls callback register to either resolve (), reject (), or notify () according to how it has completed. defer.resolve (‘Hello, ‘ + name + ‘!’); defer.reject (‘Greeting ‘ + name + ‘ is not allowed.’);
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With