What is the difference between a microtask and a macrotask?
A macrotask is a piece of work that the event loop takes from the task queue, one item at a time. A microtask is a piece of work that goes into a different queue, and that queue is emptied completely before the loop takes the next macrotask.
The difference is about priority and about how much of the queue is handled at once. The microtask queue is emptied all the way, and it includes any microtasks that those microtasks queue up. The task queue is taken one entry at a time, and the microtask queue is emptied after each one.
In practice the split looks like this. Promise reactions, the queueMicrotask function, and the MutationObserver callbacks are all microtasks. Timers, finished network requests, user events, and the first run of your script are all macrotasks.
The result is the ordering that people find surprising. A promise callback always runs before a timer callback, even when the timer was set up first and has a delay of zero. And a promise callback that is queued from inside a timer callback runs before the next timer, because the microtask queue is emptied after every task.
There is one more distinction that matters when you design code. Because the microtask queue is emptied completely, a chain of promise callbacks can starve the event loop. If every callback queues another one, then timers and input handling never run and the page appears frozen. Giving the loop a macrotask, by scheduling a timer with a delay of zero, lets the runtime render the screen and handle input again.