Suggest an editImprove this articleRefine the answer for “Blocking the current function with await”. Your changes go to moderation before they’re published.Approval requiredContentWhat you’re changing🇺🇸EN🇺🇦UAPreviewTitle (EN)Short answer (EN)**`await`** does not block the JavaScript execution thread - it only pauses the `async` function it is in, turning the rest of that function's code into a microtask while the Event Loop keeps handling other code. **Key point:** once the promise settles, the microtask returns to the queue and execution resumes exactly where `await` was, without blocking the whole application.Shown above the full answer for quick recall.Answer (EN)Image## 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). 3. The JS engine keeps running **other tasks**: other functions, event handlers, timers, and so on. 4. 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 | 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 ```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` 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".For the reviewerNote to the moderator (optional)Visible only to the moderator. Helps review go faster.