Asynchronous JavaScript
Every example so far has run top to bottom, instantly. Real pages constantly wait on things that take time — loading data from a server being the most common. JavaScript handles this with promises, and the modern, readable way to work with them: async/await. This is the final Fundamentals chapter, and it pulls together functions (Ch 5), objects (Ch 7), and the DOM (Ch 8–9) into one realistic workflow.
The Problem: JavaScript Doesn't Wait
setTimeout schedules a function to run later without pausing anything else. JavaScript moves straight on to the next line rather than waiting — this is what "asynchronous" means: code that will run eventually, not necessarily in the order it's written.
Promises — A Placeholder for a Future Value
A Promise represents a value that doesn't exist yet, but will at some point — either successfully (resolve) or with an error (reject). .then() registers a function to run once the promise resolves, receiving the resolved value as its argument.
async/await — Promises Without the .then() Chains
async before a function lets await be used inside it. await pauses that function (not the whole page) until the promise resolves, then continues with the resolved value — reading top-to-bottom almost exactly like the synchronous code from every earlier chapter, while still being genuinely non-blocking.
await in a normal function is a syntax error. This is the one new keyword pairing to remember: no async on the function, no await inside it.
fetch — Getting Real Data from a Server
fetch() requests a URL and returns a promise. The first await gets the response itself; response.json() then parses the body text into a real JavaScript object — another promise, needing its own await — at which point Chapter 7's dot/bracket notation works on it normally.
Handling Errors with try/catch
A network request can fail — no connection, server error, invalid URL. Wrapping await calls in try/catch means a failure jumps straight to the catch block instead of leaving an unhandled error. Without this, a failed request can silently break the rest of the function with no clear feedback to the user.
fetch only rejects (triggering catch) on a genuine network failure — a 404 or 500 response is still considered a "successful" fetch as far as the promise is concerned. Check response.ok (a boolean) explicitly if a non-success status code needs separate handling.
Putting It Together: Fetch + DOM
This is the complete, realistic pattern: show a loading state immediately, fetch data, then update the page with the result — or an error message if it fails. Every chapter since Chapter 5 contributes something here: functions, objects, DOM updates, and now asynchronous waiting.
Coding Challenges
Write an async function delayedGreet(name) that uses await with a Promise wrapping setTimeout (1 second) to wait, then console.logs `Hello, ${name}!`. Call it and log "Calling..." immediately before, to confirm the greeting really does log after a delay.
📄 View solutionWrite an async function getPost(id) that fetches from `https://jsonplaceholder.typicode.com/posts/${id}`, parses the JSON, and logs the post's title. Wrap it in try/catch and log "Failed to fetch post." on any error.
📄 View solutionAssume a button #loadBtn and a p#status. Add a click listener that's an async function: on click, set status text to "Loading...", await a fetch to `https://jsonplaceholder.typicode.com/users/1`, then set status to the user's email — or "Error loading data." if the fetch fails.
📄 View solutionChapter 10 Quick Reference
- Asynchronous code doesn't block — JavaScript moves on while it's pending
- Promise — a placeholder for a value that resolves (success) or rejects (failure) later
- async function — required wrapper to use await inside
- await — pauses the async function (not the page) until a promise settles
- fetch(url) — returns a promise for an HTTP response
- response.json() — parses the response body into a usable object/array
- try/catch — wraps await calls to handle network/parsing failures gracefully
- response.ok — check explicitly for non-200 status codes; fetch won't throw on its own for those
- This completes JavaScript Fundamentals. Intermediate begins with closures, the spread/rest operators, and ES6 classes.