Macro and micro tasks

Macro task The browser Node
I/O Square root Square root
setTimeout Square root Square root
setInterval Square root Square root
setImmediate x Square root
requestAnimationFrame Square root Square root
Micro tasks The browser Node
process.nextTick x Square root
MutationObserver Square root x
Promise.then / catch / finally Square root Square root

Classic example:

There is a queue in front of the bank counter, all the people are saving money, saving is macro task, this queue is macro task queue, when a “Macro uncle” was called to his number, he went to — was processed, processing deposit business, “Macro uncle” suddenly wanted to do a micro finance for his savings (micro task), Then the bank staff will add his needs to their micro task queue, uncle no longer queue, directly after the macro task of saving money on the processing of the derived micro task financial management, do financial management uncle said to do a credit card, then row to the micro task queue. However, if macro says he needs to save money, and it is his wife who wants to save money, it is also a macro task, but sorry, need to take the number to the back of the macro task queue (here is the processing method of creating micro task and macro task while macro task is in progress).

Author: Crab Yellow student link: juejin.cn/post/695636… The copyright belongs to the author. Commercial reprint please contact the author for authorization, non-commercial reprint please indicate the source.