Does the JS rack always have at least one frame?

I recently saw a presentation in the JS event loop , which, frankly, is brilliant, but now I have a lingering question about JS. If you think of a global execution context, such as main (), main () has never been resolved? My reasoning here is that if that were the case, then the JS program would be complete and no callbacks would occur.

- change

My main interest here is how the call stack is presented with respect to the callback queue. If it is assumed that the event loop waits until the call stack is empty before pushing new frames onto the stack, then the loop will wait for the program to complete and the callbacks will have no effect.

I suppose this means that the event loop waits for only one frame (the main execution context) to remain, not frames.

+4
source share
2 answers

As you said, main () is the global execution context. He is still alive while:

  • you close the tab if you are in a browser.
  • you kill the process if you are on the server (e.g. node)
  • tab / process crashes

Therefore, if one of the above cases fails, the runtime is still alive, as well as the event loop (which will process the task queue).

+2
source

If you are thinking of a global execution context, for example, main ()

. main , , . - - , , , .

main() ?

. main() - , script -, (, ). , node . , , . " ", .

, , JS .

. , script , ( , node ..) . (, , ).

(XHR, -, IO ..) , . . ( ) , , . , , . , , ( , , ), , , .

, javascript-, callstack , - , .

+3

Source: https://habr.com/ru/post/1568006/


All Articles