JavaScript async/await: Why Your console.logs Print in the "Wrong" Order
This course is for a developer who already writes JavaScript — you use .then(), you write async/await, you call setTimeout — but the order things run in still feels like magic. You log A before B in the source and B prints first; a .then() you wrote last somehow runs before a setTimeout(fn, 0) you wrote earlier; you can't say what await actually does to the line under it. The fix is not more syntax. It is one mental model: a single call stack, a microtask queue, and a task queue, with an event loop deciding who runs next. Once you can see those three places, the ordering stops being magic and becomes something you can predict on paper.
By the end you'll be able to (course objectives):
- Describe the call stack and run-to-completion, and explain why synchronous code always finishes before any deferred callback.
- Name the three moving parts — call stack, task queue, event loop — and say where a
setTimeoutcallback waits and who runs it. - Explain why a promise
.then()callback beats asetTimeout(fn, 0), using the microtask queue and its "drain fully before the next task" rule. - Explain what
awaitactually does: it suspends only its own async function and schedules the rest as a microtask, without blocking the thread. - Predict the exact output order of a non-trivial snippet mixing sync logs,
setTimeout,.then(), andawait, then verify it in a real Node or browser console.
Prerequisites: You can write JavaScript functions, call console.log and setTimeout, and you have used .then() and async/await at least syntactically. No prior event-loop knowledge, no HTML-spec reading, and no framework or build tooling are assumed.
Practice environment: You run the snippets yourself in Node (node file.mjs) or a browser DevTools console — this course has no embedded runtime. Every prediction ends with you running the real code and checking the order against your call.
Lessons
Sources listed in sources.md.