是否所有 WebAPI 都被推送到任务队列中?

Do all WebAPIs get pushed on the task queue?

我看过 Philip Roberts 关于事件循环的演讲 (https://www.youtube.com/watch?v=8aGhZQkoFbQ&ab_channel=JSConf),但我无法理解所有内容。

他说 WebAPI 不会直接在调用堆栈上执行,它们会被推送到 WebAPI 的这个特定部分,然后,当它们的计时器完成时(如果有的话),它们会被推送到任务中队列。 (关于事件循环的讨论从 12:50 开始)

https://developer.mozilla.org/en-US/docs/Web/API 上,我看到 console 也是一个 WebAPI。

console.log() 是否也被推入任务队列,或者仅执行具有特定计时器的 WebAPI,就像 setTimeout 或等待特定事件(如单击 DOM 中按钮上的事件侦听器)被推送到任务队列中?

这是我迄今为止尝试回答的问题:

在第 13:45 分钟他说:

The event loop job is to look at the stack and look at the task queue. If the stack is empty, it takes the first thing on the queue and pushed it on to the stack.

但是,如果我 运行 这个代码:

console.log("log 1");

while(true){}

console.log("log 2");

我会在控制台得到log 1,然后整个tab会因为while(true)语句被阻塞。这是否意味着直接执行 console.log() 而不是将其推入任务队列?如果它会被推送到任务队列,那么它会等待调用堆栈为空,但是,调用堆栈不能为空,因为我在那里有 while(true) 语句 运行 永远。 console.log("log 1") 仍然执行,即使 console 是 WebAPI。这是否意味着 console.log() 不会被推送到任务队列,只有具有计时器的 WebAPI,如 setTimeout,或者等待事件从 [=23] 触发,如 addEventListener =] WebAPI,被推送到任务队列中?

只有异步 WebAPI,如 DOM 事件、获取、setTimeout、setInterval 等会被推送到回调队列中。像console这样的同步WebAPI,直接执行,不入回调队列。