简体   繁体   English

回调队列和事件队列有什么区别?

[英]What is the difference between callback queue and event queue?

In some of the online resources on asynchronous behavior of JavaScript, concepts like browser architecture, call stack, event loop and event queue are also mentioned.在JavaScript的一些关于异步行为的在线资源中,也提到了浏览器架构、调用堆栈、事件循环和事件队列等概念。 While describing the workings of browser event loop, some use the word event queue while others callback queue .在描述浏览器事件循环的工作原理时,一些人使用事件队列这个词,而另一些人使用回调队列 I am confused by those terms and would like to know if they refer to the same queue data structure which is used in browsers or are they different and are used it to handle different scenarios?我对这些术语感到困惑,想知道它们是否指的是浏览器中使用的相同队列数据结构,或者它们是否不同并用于处理不同的场景?

Figure 1 -图1 -

在此处输入图像描述

Figure 2 -图 2 -

在此处输入图像描述

In the HTML's nomenclature (which defines the browser's event-loop), both are incorrect.在 HTML 的命名法(定义浏览器的事件循环)中,两者都不正确。

What we have are "task-sources" , which all may map to different task-queues .我们所拥有的是“任务源” ,它们都可能 map 到不同的任务队列
At the beginning of the event-loop processing , the User Agent will choose from which task-queue it will execute the next task .事件循环处理开始时,用户代理将选择从哪个任务队列中执行下一个任务 This task may itself fire events , or invoke callbacks .该任务本身可能触发事件,或调用回调

These tasks are queued by various means, either as part of other tasks (eg after a task started a work in parallel, it will ask to queue a task when that parallel work is done), either from IPC messages directly (eg user initiated events).这些任务通过各种方式排队,或者作为其他任务的一部分(例如,在任务开始并行工作后,当并行工作完成时,它会要求将任务排队),或者直接来自 IPC 消息(例如,用户发起的事件)。

Note also that there is a part of the event-loop where callbacks are invoked and events fired directly by the event-loop, and not from a task: Update the rendering .另请注意,事件循环的一部分调用回调并由事件循环直接触发事件,而不是来自任务: 更新呈现
In there you'll find a map of callbacks and various events (scroll, resize, mediaqueries etc.) that are called as part of this special place in the event-loop, which itself gets called only once in a while (generally when the monitor sends a V-Sync signal).在那里你会发现一个map回调和各种事件(滚动、调整大小、媒体查询等),它们在事件循环中作为这个特殊位置的一部分被调用,它本身只在一段时间内被调用一次(通常当监视器发送一个垂直同步信号)。

Inside the callback queue, the tasks are broadly classified into two categories, namely microtasks and macrotasks (often referred as tasks).在回调队列中,任务大致分为两类,即任务和宏任务(通常称为任务)。

Macrotasks get added to the macrotask queue when:在以下情况下,任务会添加到宏任务队列中:

  • A new JavaScript program or subprogram is executed (such as from a console, or by running the code in a element) directly.一个新的 JavaScript 程序或子程序被直接执行(例如从控制台,或通过运行元素中的代码)。
  • An event fires, adding the event's callback function to the macrotask queue.事件触发,将事件的回调 function 添加到宏任务队列。
  • A timeout or interval created with setTimeout() or setInterval() is reached, causing the corresponding callback to be added to the task queue.达到使用 setTimeout() 或 setInterval() 创建的超时或间隔,导致相应的回调被添加到任务队列中。

A microtask is a short function which is executed after the function or program which created it exits and only if the JavaScript execution stack is empty, but before returning control to the event loop being used by the user agent to drive the script's execution environment.微任务是一个简短的function ,它在 function 或创建它的程序退出后执行,并且仅当 JavaScript 执行堆栈为空时,但在将控制权返回给用户代理用来驱动脚本执行环境的事件循环之前。

Microtasks come solely from our code.微任务完全来自我们的代码。 They are usually created by promises: an execution of .then/catch/finally handler becomes a microtask.它们通常由承诺创建:.then/catch/finally 处理程序的执行成为微任务。 Microtasks are used “under the cover” of await as well, as it's another form of promise handling.微任务也在等待的“掩护下”使用,因为它是 promise 处理的另一种形式。

In literature you will often see that they refer to macrotasks as tasks, and microtasks as jobs.在文献中,您经常会看到他们将宏任务称为任务,将微任务称为作业。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM