Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get state of Angular deferred?

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)

like image 862
Evan Hobbs Avatar asked Jun 06 '14 22:06

Evan Hobbs


People also ask

What is the difference between $Q and deferred in angular?

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.

What is deferred&promises in AngularJS?

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.

What is state in angular?

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.

What is the return type of deferred in JavaScript?

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.’);


1 Answers

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.

like image 117
Benjamin Gruenbaum Avatar answered Sep 21 '22 05:09

Benjamin Gruenbaum