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?
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
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.
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
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.
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.
What are the practical consequences of this?
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.
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)
So when to use what?
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.
Examples
macrotasks: setTimeout, setInterval, setImmediate, requestAnimationFrame, I/O, UI rendering
microtasks: process.nextTick, Promises, Object.observe, MutationObserver
I wrote a post on this, including interactive examples https://jakearchibald.com/2015/tasks-microtasks-queues-and-schedules/
Update: I also gave a talk about this https://www.youtube.com/watch?v=cCOL7MC4Pl0. The talk goes into more detail, including how tasks & microtasks interact with rendering.
Basic concepts in spec:
- An event loop has one or more task queues.(task queue is macrotask queue)
- Each event loop has a microtask queue.
- task queue = macrotask queue != microtask queue
- a task may be pushed into macrotask queue,or microtask queue
- when a task is pushed into a queue(micro/macro),we mean preparing work is finished,so the task can be executed now.
And the event loop process model is as follows:
when call stack is empty,do the steps-
- select the oldest task(task A) in task queues
- if task A is null(means task queues is empty),jump to step 6
- set \"currently running task\" to \"task A\"
- run \"task A\"(means run the callback function)
- set \"currently running task\" to null,remove \"task A\"
- perform microtask queue
- (a).select the oldest task(task x) in microtask queue
- (b).if task x is null(means microtask queues is empty),jump to step (g)
- (c).set \"currently running task\" to \"task x\"
- (d).run \"task x\"
- (e).set \"currently running task\" to null,remove \"task x\"
- (f).select next oldest task in microtask queue,jump to step(b)
- (g).finish microtask queue;
- jump to step 1.
a simplified process model is as follows:
- run the oldest task in macrotask queue,then remove it.
- run all available tasks in microtask queue,then remove them.
- next round:run next task in macrotask queue(jump step 2)
something to remember:
- when a task (in macrotask queue) is running,new events may be registered.So new tasks may be created.Below are two new created tasks:
- promiseA.then()\'s callback is a task
- promiseA is resolved/rejected: the task will be pushed into microtask queue in current round of event loop.
- promiseA is pending: the task will be pushed into microtask queue in the future round of event loop(may be next round)
- setTimeout(callback,n)\'s callback is a task,and will be pushed into macrotask queue,even n is 0;
- task in microtask queue will be run in the current round,while task in macrotask queue has to wait for next round of event loop.
- we all know callback of \"click\",\"scroll\",\"ajax\",\"setTimeout\"... are tasks,however we should also remember js codes as a whole in script tag is a task(a macrotask) too.