Non-blocking
The page can keep responding while work is pending.
Async JavaScript lets work finish later without freezing the page.
status.textContent = "Loading...";
setTimeout(() => {
status.textContent = "Done";
}, 1000);
Async JavaScript
JavaScript runs on a single main thread in the browser, but many operations do not complete immediately. Timers, network requests, file reads and user interactions all happen over time.
Async programming is the pattern of starting work, letting the browser stay responsive and then reacting when the work completes.
Good async code always answers three questions: what is loading, what happens on success and what happens on failure.
The page can keep responding while work is pending.
A function called later.
An object representing future success or failure.
Syntax that makes promise-based code read sequentially.
Examples
status.textContent = "Loading..."; loadData() .then(render) .catch(showError);
loadData().then(render); // What happens when loading fails?
Code patterns
These examples are the async patterns you will reuse: timers, promises, composition, await, fetch and cancellation.
Run code later.
setTimeout(() => {
console.log("later");
}, 500);
React when a promise resolves.
loadData().then(data => {
render(data);
});
await pauses inside the async function only.
async function start() {
const data = await loadData();
render(data);
}
Async work needs catch or try/catch.
loadData().catch(error => {
showError(error.message);
});
Rules that matter
Async JavaScript is easier when every operation has clear pending, success, failure and cancellation behavior.
Users should know something is happening.
Network and parsing can fail.
Long synchronous work freezes interaction.
Callers need to know when work finishes.
loading, success and error should be visible in code.
Use either async/await or then chains consistently in a block.
Production thinking
Modern interfaces depend on async work. Without a clear model, loading bugs and race conditions appear quickly.
Loading and error states should be visible text, not only spinners or color changes.
Production async code should handle timeout, cancellation, retry and user feedback where appropriate.
Critical content loaded asynchronously should render reliably and not depend on fragile client-only timing.
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.