What Await Does
You'll learn: How await pauses execution in an async function until a promise resolves, giving you its value directly.
The Core Idea
The await keyword is your way of saying "stop here and wait for this promise to finish." When JavaScript encounters await inside an async function, it pauses that function's execution until the promise settles. Once the promise resolves, await extracts and returns the resolved value—no .then() needed.
Think of it like waiting in line at a coffee shop. When you say await getCoffee(), you're standing at the counter until your order is ready. The barista hands you the actual coffee (the resolved value), not a receipt promising coffee (the promise object). Meanwhile, the rest of the coffee shop keeps operating—JavaScript doesn't freeze the entire program, just your spot in line.
How It Works in Practice
When you write const result = await somePromise, JavaScript:
- Pauses execution of the async function at that line
- Waits for
somePromiseto settle - If resolved: extracts the value and assigns it to
result - If rejected: throws an error (which you can catch with try/catch)
- Resumes execution on the next line
This transforms promise-based code into something that reads sequentially, like synchronous code, even though it's still asynchronous under the hood.
Why This Matters
Without await, you'd work with the promise object itself and chain .then() calls. With await, you work directly with the values, making your code cleaner and easier to reason about.
Key Takeaway: await pauses an async function until a promise settles, then unwraps and returns the resolved value, letting you write asynchronous code that reads like synchronous code.