Frontend · interactive
Promises and async/await
await pauses only the async function it's in. The caller, and the rest of the page, keep running. Step through the order things happen, then see why awaiting one call after another is often slower than it needs to be.
- async function load() {
- log("B: load starts");
- const user = await getUser();
- log("E: got " + user);
- }
- log("A: script starts");
- load();
- log("C: load() returned a promise");
- setTimeout(() => log("F: timer"), 0);
- log("D: script ends");
output: A
Synchronous code runs top to bottom.
Show
An async function returns a promise at once
Its body runs synchronously until the first await, then the caller gets a pending promise back.
Output so far: A B C.
await parks the rest of the function
The code after await becomes a callback for when the promise settles. Nothing blocks.
Resumes as a microtask, before timers.
Start together, await together
If calls don't depend on each other, start them all and await Promise.all. Total time is the slowest, not the sum.
await Promise.all([getUser(), getOrders()])
Handle rejections
Wrap awaits in try/catch. Promise.all rejects on the first failure; use Promise.allSettled when you want every result.
An unhandled rejection is a silent bug.
In production
Tools and technologies
async/await is built into every JavaScript runtime.
Not tied to one language — use these names when you search docs or design a system.