Blocking the current function with await
Short answer
awaitdoes not block the JavaScript execution thread, it only pauses theasyncfunction it is in.
Why is that? Because JS runs code on a single thread, but manages tasks through event queues (the Event Loop). await simply inserts "pauses" into the code, turning the rest of the function into a microtask, and hands control back to the engine so it can run other code.
How this works under the hood
-
When the engine encounters
await promise, it pauses execution of the currentasyncfunction. -
Instead, it:
- creates a microtask (a callback) that will continue the function once the promise settles;
- returns control to the Event Loop (frees the stack).
-
The JS engine keeps running other tasks: other functions, event handlers, timers, and so on.
-
When the promise settles (
fulfilledorrejected), the microtask runs, and execution resumes inside that function, exactly whereawaitwas.
Example - a "pause" inside a function, not in the whole program
async function task() {
console.log('Start');
await new Promise(r => setTimeout(r, 2000));
console.log('End');
}
task();
console.log('This code runs right away!');Output:
Start
This code runs right away!
(after 2 sec)
EndExplanation:
awaitpauses only thetask()function, while the main thread keeps going.- JS does not "freeze": a microtask is simply created inside
task(), and it returns to the queue once the timer finishes.
If await blocked the thread...
Then the whole of JavaScript would "freeze" for 2 seconds: not a single event handler, not a single setTimeout, would run. But that is not what happens: while task() "waits", the Event Loop keeps handling other events.
What this looks like in the Event Loop
| Step | What happens |
|---|---|
| 1 | task() enters the Call Stack and runs the code up to await |
| 2 | At await, the function "freezes", a microtask is created |
| 3 | The stack clears, other scripts keep running |
| 4 | When the promise settles, the microtask returns to the queue |
| 5 | After the current Event Loop tick, the rest of task()'s code runs |
Example with parallel code
async function foo() {
console.log('foo start');
await new Promise(res => setTimeout(res, 1000));
console.log('foo end');
}
async function bar() {
console.log('bar start');
await new Promise(res => setTimeout(res, 500));
console.log('bar end');
}
foo();
bar();
console.log('main thread');Output:
foo start
bar start
main thread
(after 0.5s) bar end
(after 1s) foo endawait does not stop other functions from running. Each function simply "freezes" until its own promise settles, then continues independently.
Example of "resuming microtasks"
async function demo() {
console.log('A');
await Promise.resolve();
console.log('B');
}
demo();
console.log('C');Result:
A
C
BWhy:
Aruns synchronously;await"stops" the function and putsBin the microtask queue;- the main thread runs
C; - then the microtasks run,
B.
A simple analogy
Imagine an
asyncfunction is a TV show.When the episode hits an
await, the show pauses, but other shows keep airing on other channels.Then, once the needed promise is ready, the episode resumes exactly where it left off.
Summary
What await does | What it does not do |
|---|---|
Pauses only the current async function | Does not block the JavaScript thread |
| Returns control to the Event Loop | Does not stop other tasks from running |
| Resumes execution once the promise settles | Does not "freeze" the whole application |
Implemented via microtasks (PromiseJobs) | Does not use threads or sleep |
In other words, await is an asynchronous pause, not "blocking the thread".
Short Answer
Interview readyA concise answer to help you respond confidently on this topic during an interview.