简体   繁体   English

Nodejs 消息队列与宏任务队列

[英]Nodejs message queue vs macrotask queue

What is the difference between message queue and macrotask queue ?消息队列宏任务队列有什么区别? Is it the same thing?是一样的吗?

On Nodejs documentation here it says:这里的 Nodejs 文档中它说:

When setTimeout() is called, the Browser or Node.js starts the timer.调用 setTimeout() 时,浏览器或 Node.js 会启动计时器。 Once the timer expires, in this case immediately as we put 0 as the timeout, the callback function is put in the Message Queue.一旦计时器到期,在这种情况下,我们立即将 0 作为超时,回调函数被放入消息队列中。

While, in this section, it says:而在节中,它说:

A setTimeout, setImmediate callback is added to macrotask queue一个 setTimeout, setImmediate 回调被添加到宏任务队列

So, does message queue and macrotask queue is the same thing?那么,消息队列宏任务队列是一回事吗?

It refers to the same thing here.这里指的是同一个东西。 One could say that " message queue " is a more general term, as in " the microtask queue and the macrotask queue are both message queues ".可以说“消息队列”是一个更笼统的术语,如“微任务队列和宏任务队列都是消息队列”。

If I am reading this right, it seems the "Message Queue" is a general term that is implemented using a few different queues.如果我没看错的话,“消息队列”似乎是一个通用术语,它是使用几个不同的队列实现的。

In this section they explain:节中,他们解释:

A process.nextTick callback is added to process.nextTick queue.将 process.nextTick 回调添加到 process.nextTick 队列。 A Promise.then() callback is added to promises microtask queue.一个 Promise.then() 回调被添加到 Promise 微任务队列中。 A setTimeout, setImmediate callback is added to macrotask queue.将 setTimeout、setImmediate 回调添加到宏任务队列。

Looks like the macrotask, microtask, etc. are specific parts of the implementation and have different attributes.看起来宏任务、微任务等是实现的特定部分,并且具有不同的属性。

For example, execution order:例如执行顺序:

Event loop executes tasks in process.nextTick queue first, and then executes promises microtask queue, and then executes macrotask queue.事件循环先执行process.nextTick队列中的任务,然后执行promises microtask queue,再执行macrotask queue。

Found another easy explanation here . 在这里找到另一个简单的解释。

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

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