JS调用堆栈是否总是至少有一帧? [英] Does the JS callstack always have at least one frame?

查看:68
本文介绍了JS调用堆栈是否总是至少有一帧?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近在JS事件循环上看到了演示文稿 ,很棒,但是我现在对JS调用栈还有一个挥之不去的问题。如果您将全局执行上下文视为main(),则main()从未解决?我的理由是,如果可以,那么JS程序将完成,并且不会发生回调。

I've recently seen a presentation on the JS event loop which is, frankly, brilliant, but I have a lingering question now about the JS call stack. If you think about the global execution context as, say, main(), is main() never resolved? My reasoning here is that, if it were, then the JS program would be complete, and no callbacks would happen.

-edit

我的主要兴趣是相对于回调队列,如何表示调用堆栈。如果说事件循环要等到调用堆栈为空,然后再将新帧推送到堆栈上,那么循环将一直等到程序完成,并且回调不会产生任何作用。

My primary interest here is how the call stack is represented, in relation to the callback queue. If the event loop is said to wait until the call stack is empty before pushing new frames onto the stack, then the loop would be waiting until the program is complete, and callbacks wouldn't have any effect.

我想这意味着事件循环会一直等到只剩下一个帧(主要执行上下文),而不是没有帧。

I suppose that means that the event loop waits until there is only one frame left (the main execution context), not no frames.

推荐答案

正如您所说,main()是全局执行上下文。直到:

As you said, main() is the global execution context. It's still alive until :


  • 如果您在浏览器中,则关闭选项卡

  • 如果您在服务器(如节点)上,则杀死进程

  • 选项卡/进程崩溃

因此,除非发生上述用例之一,否则运行时仍然有效,事件循环(将处理任务队列)也将保留。

So unless one of the use cases above happen, the runtime is still alive, so is the event loop (which will process the task queue).

这篇关于JS调用堆栈是否总是至少有一帧?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆