Synchronous vs Asynchronous
BeginnerWhy slow work blocks everything when it runs in line, and how handing it off lets the rest keep going.Builds on Execution Context
Empty
Empty
Empty
Empty
- Running code
- Running microtasks
- Taking the next task
- Waiting
Empty
Spaceplay or pause←→previous or next stepRback to the start
Explanation
2 min read
On this page
What is the difference between synchronous and asynchronous code?
Synchronous code runs one line at a time, in order, and each line waits for the one before it to
finish. code starts a slow job, such as a timer, a network request or reading a
file, and moves on without waiting. The result comes back later, through a callback, a promise or
await.
How does asynchronous code keep the page responsive?
JavaScript has a single call stack, so it does one thing at a time.
- If slow work runs on the stack, everything else waits: the next line, clicks, scrolling and repaints. The page freezes. This is called blocking.
- If slow work is handed off to the browser or Node.js (Web APIs such as
setTimeoutandfetch), the stack is free right away. When the job is done, its callback waits in a queue until the stack is empty again.
Asynchronous does not mean parallel JavaScript. Your code still runs one piece at a time; only the waiting happens elsewhere.
What mistakes do people make with asynchronous code?
Mini example
console.log('1'); setTimeout(() => { console.log('3: after one second');}, 1000); console.log('2: does not wait');Quick check
In which order does the mini example log its lines?
Further reading
Watch the video
The full explanation from the YouTurkey Tech YouTube channel, in Arabic.
All steps as text
- Step 1:
The script goes onto the call stack. JavaScript has one stack, so it runs one thing at a time, top to bottom.
- Step 2:
order coffee is printed.
Line 1
- Step 3:
First, the synchronous way:
makeCoffeeSync()goes on top of the stack.Line 9
- Step 4:
The loop spins for three seconds. The stack stays busy, so nothing else can run: not the next line, not a click, not a repaint.
Lines 4–5
- Step 5:
The loop ends and coffee ready (sync) is printed.
Line 6
- Step 6:
Only after the function returns does read the news get printed, three seconds late. That is blocking.
Lines 7–10
- Step 7:
Now the asynchronous way:
setTimeoutis called and goes on top of the stack.Lines 12–14
- Step 8:
It hands the callback to the browser's timer and returns at once. The three seconds pass outside the call stack.
Lines 12–14
- Step 9:
read the news again is printed immediately. Nothing waited.
Line 16
- Step 10:
The script is done. The stack is free, so the page stays responsive while the timer counts.
- Step 11:
Three seconds later the timer is done, and the callback waits in the task queue.
Lines 12–14
- Step 12:
The stack is empty, so the event loop moves the callback onto it.
Lines 12–14
- Step 13:
coffee ready (async) is printed.
Line 13
- Step 14:
The callback returns and the event loop waits for new work.
Line 14
- Step 15:
Both coffees took three seconds. The synchronous one froze everything meanwhile; the asynchronous one let the rest of the code go on.