angular.js icon indicating copy to clipboard operation
angular.js copied to clipboard

API to retrieve promise state

Open cgross opened this issue 12 years ago • 26 comments

Right now $q promises don't have any way to get their state (pending, fulfilled, rejected). There's ajoslin/angular-promise-tracker that will keep track of $q promises for you but it really seems overkill to have such a project simply because there's no way to get $q promise states.

Would suggest something like Q has with isFulfilled(), isRejected() and isPending().

cgross avatar Apr 16 '14 21:04 cgross

+1

aaronleesmith avatar May 02 '14 02:05 aaronleesmith

+1

ghost avatar May 02 '14 02:05 ghost

+1 I was looking for where I could get $q.isPending, similar to Q.isPending and ended up here.

ericeslinger avatar May 09 '14 23:05 ericeslinger

+1

costin-zaharia avatar Jun 18 '14 14:06 costin-zaharia

Is there a specific reason you need the state of a promise? Because in general, you can just chain on .then().

lord2800 avatar Jun 19 '14 17:06 lord2800

When you want to have an element visible during the duration of a promise.

ng-show="myPromise.isPending()"

cgross avatar Jun 19 '14 21:06 cgross

Got it. It's not exactly elegant, but what about:

$scope.isDone = false;
myPromise.then(function () { $scope.isDone = true; });

? Like I said, quite ugly, but it would suffice for now.

lord2800 avatar Jun 19 '14 22:06 lord2800

Thanks. We have various solutions today. This issue requests a simpler straightforward way.

cgross avatar Jun 19 '14 22:06 cgross

+1

rinatio avatar Jun 23 '14 16:06 rinatio

Hey guys, I'm trying to create some kind of flag if promise is resolved or not by decorating angular $q service, but it doesn't seem to be working. Here's a gits with decorator code. I need this flag to show loading indicator when ajax is in pending and I need it for many requests so it's better to keep it DRY. Since $http uses not decorated $q there's probably no other way to make it except add it to the core. Please advise

rinatio avatar Jun 25 '14 11:06 rinatio

Sorry guys, finally got it working, my fault. Having a common api is better anyway I think. Thanks!

rinatio avatar Jun 25 '14 12:06 rinatio

+1

xeronimus avatar Dec 02 '14 12:12 xeronimus

+1. Of note, this data is actually present in the current version, but is not documented. promise.$$state contains all the information we need. However, I worry that its not being documented means we're not supposed to be using it and it is likely to change. Having a consistent documented API for this would be awesome.

ashaffer avatar Dec 02 '14 21:12 ashaffer

Please don't use Promise#$$state ^_^ It would be a private symbol if we could change our supported browsers :d

caitp avatar Dec 02 '14 22:12 caitp

(but seriously --- please don't use that in production, it's not going to result in well-designed code. However, I think that is a workable solution for this issue, so we can probably close this).

caitp avatar Dec 02 '14 22:12 caitp

...myep I'm talking to myself, but gonna close this one for now unless people feel like touching private variables just isn't good enough.

caitp avatar Dec 02 '14 22:12 caitp

Is it? Do you disagree that it would be desirable to have a codified API similar to this?

ashaffer avatar Dec 02 '14 22:12 ashaffer

@ashaffer the issue with such apis is, people tend to use them like this:

function ambiguouslySyncOrAsync(promise) {
  if (promise.isPending()) {
    // do async pathway
  } else {
    // do synchronous pathway
  }
}

And this is a pattern that you really don't want to have. Checking the promise state can certainly be valuable during testing, but is generally something you want to avoid, just to make sure code is structured in a simple and sane way.

caitp avatar Dec 02 '14 22:12 caitp

@caitp Ya I agree with that, it's just pretty nice for certain things. Testing is one instance, and also showing spinners and other loading related status information. It can also be nice for showing/hiding things at the appropriate times.

ashaffer avatar Dec 02 '14 22:12 ashaffer

I would really appreciate this being reopened. My use case is to show something in the UI while a promise is running. So put the promise on scope and reference an isPending() method or similar. This need is common enough that multiple projects have sprouted up to try to handle this including angular-promise-tracker and my angular-busy. But we have to write really nasty code to determine promise state instead of having a simple method.

Most promise libraries that I look at including Q, bluebird, and RSVP all include some way to synchronously determine a promise's state.

cgross avatar Dec 02 '14 22:12 cgross