Async Fetch &
UI Updates
Learn how to request data from APIs using fetch, handle responses with async/await, and dynamically update the DOM with the results.
What Is Asynchronous Code?
JavaScript is single-threaded: it runs one instruction at a time. When you need to do something slow, like fetching data from a server, you don't want the entire page to freeze while waiting.
Asynchronous code lets JavaScript start a task and move on to other work. When the slow task finishes, a callback fires and your code picks up where it left off.
The fetch() API
fetch() is a built-in browser function that sends an HTTP request and returns a Promise. A Promise is an object that represents a value that may not exist yet but will resolve in the future.
A Promise Has Three States
The async/await Pattern
async/await is syntactic sugar over Promises. It lets you write asynchronous code that reads like synchronous code: top to bottom, no nested callbacks.
.then() Chains vs async/await
| .then() Chains | async/await | |
|---|---|---|
| Readability | Nested callbacks, harder to follow | Linear, top-to-bottom |
| Error handling | .catch() at end of chain | try/catch block |
| Debugging | Stack traces can be confusing | Stack traces are clearer |
| Under the hood | Promises | Also Promises! |
Error Handling with try/catch
Network requests can fail: the server might be down, the URL might be wrong, or the user might be offline. Always wrap your fetch calls in a try/catch block.
The Fetch-Parse-Render Flow
How to Apply the Pattern
Example 1: Fetch a Single User
Example 2: Render a List of Posts
Example 3: Loading & Error States
DOM Update Techniques
Why This Matters
Almost every modern web application fetches data from an API: social media feeds, weather widgets, search results, user profiles. Understanding async/await with fetch is the foundation for:
Common Mistakes
Summary Cheat Sheet
| Concept | What It Does | Key Detail |
|---|---|---|
| fetch(url) | Sends an HTTP request | Returns a Promise |
| response.ok | Boolean: was it 200-299? | Check before parsing |
| response.json() | Parses body as JSON | Also returns a Promise |
| async | Marks function as async | Always returns a Promise |
| await | Pauses until Promise settles | Only inside async functions |
| try/catch | Handles errors | Catches network + thrown errors |
Question 1 of 5
What does fetch() return?
Question 2 of 5
What happens if you write const data = fetch(url) without the await keyword?
Question 3 of 5
Why should you check response.ok after a fetch call?
Question 4 of 5
Which DOM method is the safest way to display API data that may include user-generated content?
Question 5 of 5
Consider this code:
async function loadPosts() {
const res = await fetch('/api/posts');
const posts = await res.json();
const ul = document.getElementById('list');
posts.forEach(post => {
const li = document.createElement('li');
li.textContent = post.title;
ul.appendChild(li);
});
}
What is missing from this function that could cause problems in production?