3

On this page, http://docs.angularjs.org/guide/concepts , in the "runtime" description there is a statement:

The $evalAsync queue is used to schedule work which needs to occur outside of current stack frame, but before the browser's view render. This is usually done with setTimeout(0), but the setTimeout(0) approach suffers from slowness and may cause view flickering since the browser renders the view after each event.

Could anyone explain what does occur outside of stack frame, but before the browser's view render means?

Kartik
  • 1,506
  • 2
  • 17
  • 28
  • 3
    In my opinion that's not the correct term to use. I think, "... outside of the current thread of execution" would be much more accurate. A "stack frame" is a function activation record; in JavaScript in particular using the term "stack frame" is questionable, and more so here. – Pointy Aug 31 '13 at 14:43
  • Also, I'm not sure there's any guarantee that a 0ms timeout will run before the browser decides to repaint the page (or a portion of the page). I suppose it might be true in some browsers. – Pointy Aug 31 '13 at 14:45
  • See also http://stackoverflow.com/questions/17301572/angularjs-evalasync-vs-timeout – Mark Rajcok Aug 31 '13 at 15:02

1 Answers1

2

You can think of stack as of a ordered list of functions run by the Javascript interpreter. On example:

function a () { b();  }
function b () { c(); }
function c () { /* POINT A */ throw new Error('x'); }
a();

In point A, stack will be (it'll be actually printed by the exception thrown):

  1. c
  2. b
  3. a

(the most recent function on top).

The $evalAsync means that the task will be called outside of the stack. For example:

function a () { $evalAsync(b); c(); } 
function b () { c(); }
function c () { /* POINT A */ throw new Error('x'); }
a();

stack could look like:

  1. c
  2. b

notice there is no a() here - b() was called outside of stack. In practice this means:

  • b() will be called after c() event though the $evalAsync(b) is called before c().
  • the call stack for b() will not contain a(). It can make it more diffucult in debugging.
kamituel
  • 34,606
  • 6
  • 81
  • 98