Skip to main content

Blocking the current function with await

Short answer

await does not block the JavaScript execution thread, it only pauses the async function 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

  1. When the engine encounters await promise, it pauses execution of the current async function.

  2. 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).
  1. The JS engine keeps running other tasks: other functions, event handlers, timers, and so on.

  2. When the promise settles (fulfilled or rejected), the microtask runs, and execution resumes inside that function, exactly where await was.


Example - a "pause" inside a function, not in the whole program

javascript
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:

javascript
Start This code runs right away! (after 2 sec) End

Explanation:

  • await pauses only the task() 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

StepWhat happens
1task() enters the Call Stack and runs the code up to await
2At await, the function "freezes", a microtask is created
3The stack clears, other scripts keep running
4When the promise settles, the microtask returns to the queue
5After the current Event Loop tick, the rest of task()'s code runs

Example with parallel code

javascript
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:

javascript
foo start bar start main thread (after 0.5s) bar end (after 1s) foo end

await does not stop other functions from running. Each function simply "freezes" until its own promise settles, then continues independently.


Example of "resuming microtasks"

javascript
async function demo() { console.log('A'); await Promise.resolve(); console.log('B'); } demo(); console.log('C');

Result:

javascript
A C B

Why:

  • A runs synchronously;
  • await "stops" the function and puts B in the microtask queue;
  • the main thread runs C;
  • then the microtasks run, B.

A simple analogy

Imagine an async function 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 doesWhat it does not do
Pauses only the current async functionDoes not block the JavaScript thread
Returns control to the Event LoopDoes not stop other tasks from running
Resumes execution once the promise settlesDoes 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 ready
Premium

A concise answer to help you respond confidently on this topic during an interview.