Async Function Execution Model
What you'll learn: How async functions pause execution at await, release control back to the event loop, and resume seamlessly when awaited Promises settle.
How Async Functions Behave
When you call an async function, it starts executing synchronously just like any other function. It runs line-by-line until it hits an await keyword. At that moment, something special happens:
- Pause: The function freezes at that exact line
- Yield Control: JavaScript returns control to the caller, allowing other code to run
- Wait: The function waits for the awaited Promise to settle
- Resume: Once settled, execution picks up right where it left off
Think of it like watching a movie with a pause button. You hit pause (await), go make popcorn (other code runs), then press play again when you're ready (Promise resolves).
The Execution Flow
async function fetchUser() {
console.log('Starting...'); // Runs immediately
const user = await getFromDatabase(); // Pauses here
// Control returns to caller
// Other code can run
// When Promise settles, resume below:
console.log('Got user:', user); // Resumes when ready
return user;
}
The key insight: async functions don't block the JavaScript thread. While waiting for a Promise, the event loop is free to handle other tasks—UI updates, timers, other async operations. This non-blocking behavior is what makes JavaScript efficient for I/O-heavy operations.
Why This Matters
This execution model lets you write asynchronous code that looks synchronous. You get the readability of step-by-step code without freezing your application. Each await is a controlled pause, not a stop.
Key Takeaway: Async functions pause at await, yielding control to the event loop, then resume execution when the awaited Promise settles—creating non-blocking, sequential-looking code.