Search code examples
angularjsangular-promiseangularjs-digest

Do things called in .then of a $q promise chain get applied in a digest?


If I use Angular's $q, do I have to worry about using $evalAsyn or $apply? Or is that handled automatically by $q?


Solution

  • Commonly, you don't need to worry about the $digest/$apply/$evalSync things in angular. According to $q document here,

    There is a short comparison:

    Differences between Kris Kowal's Q and $q

    There are two main differences:

    • $q is integrated with the $rootScope.Scope Scope model observation mechanism in angular, which means faster propagation of resolution or rejection into your models and avoiding unnecessary browser repaints, which would result in flickering UI.
    • Q has many more features than $q, but that comes at a cost of bytes. $q is tiny, but contains all the important functionality needed for common async tasks.

    Here is the source code in $q.

    this.$get = ['$rootScope', '$exceptionHandler', function($rootScope, $exceptionHandler) {
        return qFactory(function(callback) { //invoke qFactory
          $rootScope.$evalAsync(callback); //$evalAsync here...
        }, $exceptionHandler);
    }];
    function $$QProvider() {
      this.$get = ['$browser', '$exceptionHandler', function($browser, $exceptionHandler) {
        return qFactory(function(callback) {
          $browser.defer(callback);
        }, $exceptionHandler);
      }];
    }