0

I learned from this post that angular resolved promises cannot execute their 'then' function until next digest cycle: Angular JS: Chaining promises and the digest cycle

Code of that original question is here http://jsfiddle.net/QZM4d

But there is something about the answer to that post I don't understand. First, why clicking a button that does nothing in its ng-click trigger a digest cycle, while clicking the original ng-click="go" button a second time does not trigger a digest cycle?

Second, if I remove the $scope.$apply wrapper in the setTimeout call like this:

setTimeout(function() {data.resolve("Some Data")}, 2000);

Then clicking "Click me" button after 2 seconds would cause all three names to be displayed correctly, why?

I somehow suspect it's because angular does not trigger digest cycle if no value has changed, but couldn't explain why the second time I click that "Click me" button would not change the value of name3.

Community
  • 1
  • 1
axings
  • 133
  • 7
  • 1
    would be easier to comprehend the problem, if you share the code. – Vladimir M Nov 29 '16 at 20:01
  • something doesn't add up here. `setTimeout` isn't angular aware, so you really shouldn't be using it with angular at all. Aside from that, using a timeout at all to have to respond to a promise sounds like a flawed design, or more likely the promises themselves aren't angular aware. I haven't read the related question thoroughly yet, but your assertion that `.then` isn't processed until the next cycle doesn't sound accurate. – Claies Nov 29 '16 at 20:11
  • also, that answer was posted in 2013, and angular has matured and changed immensely in the last 3 years, which would explain why what you are describing doesn't sound like how modern angular releases function. – Claies Nov 29 '16 at 20:13
  • Code of that original question is here http://jsfiddle.net/QZM4d/ – axings Nov 29 '16 at 22:09
  • I still contend that this isn't really a valid question, since the *very first* thing that is recommended when encountering strange rendering behavior is to update to the latest, supported version of the framework. The fiddle you posted is using Angular 1.0.4 (2012), while the current release is 1.5.9. – Claies Nov 29 '16 at 23:33
  • on top of this, the angular documentation recommends against using `setTimeout`, and provides an alternate, `$timeout`, which is angular aware. This code, and the issue you are describing, are all surrounding out of date frameworks and incompatible methods. Overall, this feels like an [XY Problem](http://meta.stackexchange.com/a/66378/297619). You aren't describing a problem to be solved, you are describing a concern about a possible solution to an unknown real problem, and a poorly designed out of date solution at that. – Claies Nov 29 '16 at 23:37

0 Answers0