QJavaScript · Interview preparation
How do tasks and microtasks differ in the JavaScript event loop?
Understand why Promise callbacks often run before timers scheduled for the same turn.
The idea to remember.
After the current JavaScript stack finishes, the runtime drains the microtask queue before moving to the next task. Promise reactions and queueMicrotask callbacks are microtasks, while timers and many browser events enter task queues. A long chain of microtasks can delay the next task and rendering opportunity.
01Learn by doing
From first example to real project.
Start with the smallest working idea, examine a more detailed example, then look at a real application pattern. Adapt dependencies, error handling and data models to your project.
Basic: promise resolution
01 / BEGINNERPromise callbacks run after synchronous code.
console.log('A');
Promise.resolve().then(() => console.log('C'));
console.log('B'); // A, B, CIntermediate: await and errors
02 / INTERMEDIATEHandle unsuccessful HTTP responses as well as rejected requests.
async function loadUser(id) {
const response = await fetch('/api/users/' + id);
if (!response.ok) throw new Error('Unable to load user');
return response.json();
}Real scenario: parallel independent calls
03 / REAL SCENARIOPromise.all starts independent calls together and handles failures.
async function loadDashboard() {
const [account, activity] = await Promise.all([
fetch('/api/account').then(r => { if (!r.ok) throw Error('Account'); return r.json(); }),
fetch('/api/activity').then(r => { if (!r.ok) throw Error('Activity'); return r.json(); })
]);
return { account, activity };
}02Check your understanding
Try it in your own words.
Explain how do tasks and microtasks differ in the JavaScript event loop without looking at the code. Then modify the intermediate example, describe one trade-off and identify when the real-world pattern fits.
Keep learning here.
Explore more in-depth guides, exercises and related interview questions in this library.
Browse JavaScript study guides ↗