JavaScript 𝗠𝗶𝗰𝗿𝗼𝘁𝗮𝘀𝗸 𝗤𝘂𝗲𝘂𝗲 and 𝗠𝗮𝗰𝗿𝗼𝘁𝗮𝘀𝗸 𝗤𝘂𝗲𝘂𝗲

Nhan Nguyen - Jun 29 - - Dev Community

JavaScript has two secret queues to handle tasks: 𝗠𝗶𝗰𝗿𝗼𝘁𝗮𝘀𝗸 𝗤𝘂𝗲𝘂𝗲 and 𝗠𝗮𝗰𝗿𝗼𝘁𝗮𝘀𝗸 𝗤𝘂𝗲𝘂𝗲.

Image description

✨𝗠𝗶𝗰𝗿𝗼𝘁𝗮𝘀𝗸 𝗤𝘂𝗲𝘂𝗲: It's like a super-fast line where tiny tasks wait their turn. These tasks are usually promises or mutation observers. When a promise resolves or a mutation happens, they jump into the Microtask queue.

✨ 𝗠𝗮𝗰𝗿𝗼𝘁𝗮𝘀𝗸 𝗤𝘂𝗲𝘂𝗲: It's like a regular line for more significant tasks. Think setTimeout, callbacks, or fetching data from a server. When these tasks need handling, they join the Macrotask Queue.

JavaScript always finishes what it's doing before checking these queues. But it prioritizes 𝗠𝗶𝗰𝗿𝗼𝘁𝗮𝘀𝗸 over 𝗠𝗮𝗰𝗿𝗼𝘁𝗮𝘀𝗸.


I hope you found it helpful. Thanks for reading. 🙏

Let's get connected! You can find me on:

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .