0

Is it a 'fake' stack representing execution contexts of lexical environments required to run the currently executing task?

example:

Code that follows await _expression_ is placed into the microtask queue.

When it is placed onto the stack for execution, does the chrome debugger represent this task's lex. environments execution contexts in a stack above the line?

i.e. the ---------await in f2 (async)------ line?

enter image description here

AnonEq
  • 267
  • 2
  • 9
  • Yes, it is a fake stack. This was answered in your previous question by the statement [Chrome's devtools tries to be helping in maintaining an "async" stack, but this isn't the real stack. The real stack is everything before the first "async" line.](https://stackoverflow.com/a/56852989/1541563) – Patrick Roberts Jul 02 '19 at 15:54
  • 1
    @PatrickRoberts What you've referenced states this is a 'fake stack' - its highest level description. This question pertains to: 'what does this 'fake stack' actually represent'? – AnonEq Jul 02 '19 at 15:58
  • 1
    It attempts to make debugging `async` functions more consistent with the experience of debugging synchronous functions. Hypothetically, if synchronous versions of the functions were written by removing the `async` and `await` keywords, that stack would be identical to the "fake stack" presented for their asynchronous counterparts. – Patrick Roberts Jul 02 '19 at 16:02
  • @PatrickRoberts That’s neat, thank you.

 Q: Isn’t the call stack (aka execution context stack) momentarily empty prior to microtask queue (or any task queue) processing? If so (and considering JS' 'run to completion' model), why are execution contexts (i.e. f1 & start above) present below the line - aka in the ‘real stack’ - during microtask processing? Are these placed onto the real stack because the funning async function's scope chain references their lexical environment? – AnonEq Jul 02 '19 at 16:26
  • 1
    First, a correction (unless I've misunderstood you). Given that the screenshot is during the execution of the microtask, _below_ the line is the fake stack, not the real one. Only `f2` is part of the real stack. To answer your questions, yes the call stack is momentarily empty before the execution of the microtask, but the lexical environments remain because the pending, and now executing, microtask still references their scope. However, if there is no reference to a lexical environment, that does not guarantee it would be omitted from the fake stack, that's an optimization. – Patrick Roberts Jul 02 '19 at 16:34
  • @PatrickRoberts Great catch. I interpreted this statement incorrectly, "The real stack is everything before the first "async" line." This now makes sense. You've answered my question, and I'm sure your clarity here would be useful to others. – AnonEq Jul 02 '19 at 16:46
  • For any future onlooker, my statement: "Isn’t the call stack (aka execution context stack) momentarily empty prior to microtask queue (or any task queue) processing?" is inaccurate in the general sense. The entire microtask queue will be processed at the completion of each task, and completion tends to coincide with - but does not entail - an empty call stack. – AnonEq Jul 02 '19 at 17:02

0 Answers0