Inside the callback queue, the tasks are broadly classified into two categories, namely microtasks and macrotasks (often referred as tasks).
Macrotasks get added to the macrotask queue when:
- A new JavaScript program or subprogram is executed (such as from a console, or by running the code in a element) directly.
- An event fires, adding the event's callback function to the macrotask queue.
- A timeout or interval created with setTimeout() or setInterval() is reached, causing the corresponding callback to be added to the task queue.
A microtask is a short function which is executed after the function or program which created it exits and only if the JavaScript execution stack is empty, but before returning control to the event loop being used by the user agent to drive the script's execution environment.
Microtasks come solely from our code. They are usually created by promises: an execution of .then/catch/finally handler becomes a microtask. Microtasks are used “under the cover” of await as well, as it’s another form of promise handling.
In literature you will often see that they refer to macrotasks as tasks, and microtasks as jobs.