Angular.js: $q vs native JS Promise

Created on 5 Dec 2014  路  16Comments  路  Source: angular/angular.js

Other than the lack of support for older browsers with the native Promises API in browsers, what are the differences between $q and native JavaScript Promises?

Most helpful comment

$q handlers will be evaluated during a digest, so if they change app bindings, the page will be updated.

All 16 comments

$q handlers will be evaluated during a digest, so if they change app bindings, the page will be updated.

And native promises will have to make use of $scope.$apply(); to get bindings updated? Are there any plans (or thoughts) to switch to a pluggable Promise library for Angular? Something like angular.promise = window.Promise || angular.$q; would be really nice because of the flexibility it allows as long as the library conforms to A+ standards.

it is possible to "plug" certain custom promise libraries (demonstrated by @jeffbcross) --- but only if you are able to have control over their "next tick" behaviour (which is not the case for es6 promises)

I understand, is there a list of the compatible promise libraries? And how does the plugging in work? Are there any caveats?

There's no list --- @jeffbcross did it with Bluebird, there may be others but we aren't really tracking them. The plugging just works by decorating $q.

Here is the basic plnkr with Bluebird: http://plnkr.co/edit/QLM5ypuSQIsAN5BP6Spg?p=info

It works because Bluebird is kind enough to expose the setScheduler API.

@jeffbcross Thanks!

On a side note:

How come

var userPromise = User.query();
userPromise
  .$promise
  .then(function (users) {
    $rootScope.users = users;
  });

$q.
  all([userPromise, deskPromise, portfolioPromise, adminUserPromise])
  .then(function(promises) {
    console.log(promises);
  });

resolves before any of the promises in the array resolve? I put a debugger in each one, and the $q.all seems to get resolved before the other $resource requests finish, and I checked the network tab to verify.

UPDATE:

added a little more about the other promises. All the promises are handled in the same way as the userPromise, and they are all created before $q.all is run.

I also checked the $resolved in each of the promises in the promises array, and they were all false. Am I missing something?

@caitp @jeffbcross any idea what's the issue with that snippet above?

there's absolutely no way to know what's wrong just from reading that snippet

Sorry, added a little more detail. Are there any possible issues that come to mind? I just need something to work off of, SO hasn't really had any issues like this, and unfortunately I can't access SO from the company network.

SO actually had one issue, but it said that it was fixed by matching up angular-resource version with angular version. Currently I'm running 1.3.5, are you guys aware of any other possible solutions?

We aren't really sure what you're trying to do, and using a third party promise library is not technically a supported use case --- you need to investigate and solve the issue on your own, basically.

The issue tracker isn't the right place for these kinds of questions, I recommend asking on #angularjs

I haven't changed it to a third party library, this is still using $q though. That's why I'm confused.

then you should post a reproduction of your issue and file a bug about that --- it is not related to the original issue which you posted

Gotcha, I wasn't sure if it was a bug, or just something quick like an issue with ordering or syntax. I'll post an issue. Thanks.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

brijesh1ec picture brijesh1ec  路  3Comments

butchpeters picture butchpeters  路  3Comments

ashclarke picture ashclarke  路  3Comments

WesleyKapow picture WesleyKapow  路  3Comments

ashish91 picture ashish91  路  3Comments