最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - What is the difference between callback queue and event queue? - Stack Overflow

programmeradmin7浏览0评论

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. 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 -

Figure 2 -

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. 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 -

Figure 2 -

Share Improve this question edited May 16, 2021 at 17:52 Brian Tompsett - 汤莱恩 5,89372 gold badges61 silver badges133 bronze badges asked May 16, 2021 at 7:26 Aravinda MeewalaarachchiAravinda Meewalaarachchi 2,6391 gold badge30 silver badges27 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 5

In the HTML's nomenclature (which defines the browser's event-loop), both are incorrect.

What we have are "task-sources", which all may map to different task-queues.
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 (e.g 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 (e.g user initiated events).

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).

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.
  • An event fires, adding the event's callback function to the macrotask queue.
  • A timeout or interval created with setTimeout() or setInterval() is reached, causing the corresponding callback to be added to the task queue.

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.

Microtasks e solely from our code. They are usually created by promises: an execution of .then/catch/finally handler bees a microtask. Microtasks are used “under the cover” of await as well, as it’s another form of promise handling.

In literature you will often see that they refer to macrotasks as tasks, and microtasks as jobs.

发布评论

评论列表(0)

  1. 暂无评论