事件循环上下文中微任务和宏任务之间的区别 [英] Difference between microtask and macrotask within an event loop context

查看:279
本文介绍了事件循环上下文中微任务和宏任务之间的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚阅读了Promises / A +规范并偶然发现了微任务和macrotask这两个术语:请参阅 http://promisesaplus.com/#笔记

I've just finished reading the Promises/A+ specification and stumbled upon the terms microtask and macrotask: see http://promisesaplus.com/#notes

我以前从未听说过这些条款,现在我很好奇它的区别是什么?

I've never heard of these terms before, and now I'm curious what the difference could be?

我已经尝试在网上找到一些信息,但我发现的所有内容都来自w3.org档案馆(这并不能解释我与众不同之处): http://lists.w3.org/Archives/Public/public-nextweb/2013Jul/0018.html

I've already tried to find some information on the web, but all I've found is this post from the w3.org Archives (which does not explain the difference to me): http://lists.w3.org/Archives/Public/public-nextweb/2013Jul/0018.html

此外,我发现了一个名为macrotask的npm模块: https://www.npmjs.org/package/macrotask
再次,没有说明确切的区别是什么。

Additionally, I've found an npm module called "macrotask": https://www.npmjs.org/package/macrotask Again, it is not clarified what the difference exactly is.

我所知道的是,它与事件循环有关,如https://html.spec.whatwg.org/multipage/webappapis.html#task-queue
https://html.spec.whatwg.org/multipage/webappapis.html#perform-a -microtask-checkpoint

All I know is, that it has something to do with the event loop, as described in https://html.spec.whatwg.org/multipage/webappapis.html#task-queue and https://html.spec.whatwg.org/multipage/webappapis.html#perform-a-microtask-checkpoint

我知道理论上我应该能够根据WHATWG规范自己提取差异。但我确信其他人也可以从专家的简短解释中获益。

I know I should theoretically be able to extract the differences myself, given this WHATWG specification. But I'm sure that others could benefit as well from a short explanation given by an expert.

推荐答案

一次复出事件循环将从 macrotask队列处理正好一个任务(此队列在任务队列 https://html.spec.whatwg.org/multipage/webappapis.html#task-queue =noreferrer> WHATWG规范)。
完成此macrotask之后,将处理所有可用的微任务,即在相同的复飞周期内。在处理这些微任务时,它们可以排队甚至更多的微任务,这些微任务将一个接一个地运行,直到微任务队列耗尽为止。

One go-around of the event loop will have exactly one task being processed from the macrotask queue (this queue is simply called the task queue in the WHATWG specification). After this macrotask has finished, all available microtasks will be processed, namely within the same go-around cycle. While these microtasks are processed, they can queue even more microtasks, which will all be run one by one, until the microtask queue is exhausted.

如果 microtask 以递归方式排队其他微任务,可能需要很长时间才能处理下一个macrotask。这意味着,您最终可能会在应用程序中出现阻止的UI或一些已完成的I / O空闲。

If a microtask recursively queues other microtasks, it might take a long time until the next macrotask is processed. This means, you could end up with a blocked UI, or some finished I/O idling in your application.

但是,至少关于Node.js的process.nextTick函数(排队微任务),有一种内置的防止这种阻塞的方法process.maxTickDepth。此值设置为默认值1000,在达到此限制后可以进一步处理微任务,从而允许处理下一个 macrotask

However, at least concerning Node.js's process.nextTick function (which queues microtasks), there is an inbuilt protection against such blocking by means of process.maxTickDepth. This value is set to a default of 1000, cutting down further processing of microtasks after this limit is reached which allows the next macrotask to be processed)

基本上,当你需要在一个异步的时候使用微任务同步方式(即当你说在最近的将来执行这个(微)任务时)。
否则,坚持 macrotasks

Basically, use microtasks when you need to do stuff asynchronously in a synchronous way (i.e. when you would say perform this (micro-)task in the most immediate future). Otherwise, stick to macrotasks.

macrotasks: setTimeout,setInterval,setImmediate,requestAnimationFrame,I / O,UI呈现

microtasks: process.nextTick,Promises,Object.observe,MutationObserver

macrotasks: setTimeout, setInterval, setImmediate, requestAnimationFrame, I/O, UI rendering
microtasks: process.nextTick, Promises, Object.observe, MutationObserver

这篇关于事件循环上下文中微任务和宏任务之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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