Chapter XVIII · Async / Await
await
The `await` keyword pauses the execution of an async function until a Promise settles, unwrapping its resolved value.
01 How It Works
Think of pausing a movie playback until your food delivery arrives at your door.
Inside an async function, `await` suspends execution of that function block without blocking the main thread execution stack.
example.js
async function run() {
let result = await Promise.resolve("Resolved Data");
console.log(result);
}
run();
1. Place `await` before a promise expression.
2. Suspend async function execution context temporarily.
3. Resume execution and return resolved value once promise settles.
02 Practical Example
Here is how you might see this concept applied in real-world code:
practical.js
async function fetchUser() {
let response = await fetch("https://api.example.com/data");
let data = await response.json();
console.log(data);
}
Key Takeaway
The `await` keyword allows asynchronous code to be written with clean, synchronous-looking syntax.