async function
Always returns a promise.
async and await make promise-based code read like a sequence while still remaining asynchronous.
async function load() {
const data = await loadData();
render(data);
}
Async JavaScript
An async function always returns a promise. Returning a value fulfills that promise. Throwing an error rejects it.
await waits for a promise to settle before continuing inside the async function.
Use try/catch around awaited work when you need to handle rejection locally.
Always returns a promise.
Waits for a promise inside async code.
Handles awaited rejections.
Becomes the fulfilled value of the async function.
Examples
async function start() {
try {
const data = await loadData();
render(data);
} catch (error) {
showError(error.message);
}
}
async function start() {
const data = await loadData();
render(data);
}
Code patterns
These examples are the async patterns you will reuse: timers, promises, composition, await, fetch and cancellation.
Readable sequential flow.
const data = await loadData();
try/catch catches awaited rejection.
try {
const data = await loadData();
} catch (error) {
showError(error.message);
}
Return value fulfills the promise.
async function getTitle() {
return "Dashboard";
}
Start promises before awaiting both.
const profilePromise = loadProfile(); const settingsPromise = loadSettings(); const profile = await profilePromise; const settings = await settingsPromise;
Rules that matter
Async JavaScript is easier when every operation has clear pending, success, failure and cancellation behavior.
Callers still need await or then.
Awaited rejections behave like thrown errors.
Start independent promises before awaiting.
It should clear after success or failure.
It is available in modules, not classic scripts.
Async helpers should resolve to something callers need.
Production thinking
async/await is the most readable way to write many modern async workflows, but the promise model still matters.
Awaited UI updates should still show pending and error text.
Production async/await code needs cancellation, error reporting and consistent state cleanup.
Awaited rendering failures should not leave critical content missing without fallback.
Live code lab
The preview runs inside an isolated iframe. The JavaScript is placed inside the HTML editor for now, so every example stays together and remains easy to understand.
Mini assignment
Practice assignment
Try it yourself
Self-check
If you can explain when the code runs, how it fails and what happens if it becomes irrelevant, you understand the async model.
Senior audit upgrade
await pauses inside the async function, not the whole browser. That keeps the interface responsive.
async function starts -> await promise -> function pauses -> browser keeps working -> function resumes with value or throws