简体   繁体   中英

is angular's digest cycle asynchronous?

I have the following piece of code:

$scope.$apply(function () {
    $scope.$emit("connectionadd.flowchart", data);
});

if (data.cancel) {
    return false;
} else {
    return true;
}

I'm wondering whether the execution can reach if (data.cancel) line before everything inside $scope.$apply callback is executed? If $digest is async, than it's possible.

No its not, pseudo code:

function $apply(expr) {
  try {
    return $eval(expr);
  } catch (e) {
    $exceptionHandler(e);
  } finally {
    $root.$digest();
  }
}

So the answer is that it will not reach if (data.cancel) before completing, although it is possible to make that happen with $scope.$applyAsyncc

$digest is also not async, but even it was purely by answering your question, then callback still would be executed before, because digest is called after the callback execution.

You can review $digest code here: https://github.com/angular/angular.js/blob/master/src/ng/rootScope.js

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM