Chapters & contents Lesson 3 of 15
3 min read
JavaScript and TypeScriptExpanded reading

Values and asynchronous boundaries

Separate JavaScript runtime behavior from TypeScript checking.

What you’ll learn

  • Trace an object reference
  • Handle a rejected promise
  • Describe what a type annotation cannot validate

Start with the runtime value

JavaScript values exist when the program runs. TypeScript adds a checking layer that can describe expected shapes before execution, but its ordinary type annotations are removed from the generated JavaScript. This difference matters whenever data arrives from a form, a file, or an API. A declaration that a response is a particular type is not itself a runtime check. Inspect or validate external data where it enters your application before relying on its fields.

Assignment is not an object copy

Consider two variables referring to the same settings object. Assigning one variable to the other copies the reference value rather than cloning the object. Updating a property through either reference can therefore be visible through the other. When isolation is needed, choose a copy strategy that matches the structure and the intended ownership. A shallow spread creates a new outer object but still shares nested objects. Trace nested references explicitly before assuming a copy is independent.

A promise represents a future result

An asynchronous operation can complete later with a value or fail with a reason. A promise provides a way to attach behavior to those outcomes. Awaiting a promise pauses the containing async function until it settles; it does not freeze every other task in the environment. The rest of your program may continue while the result is pending. Keep the user interface honest about loading, success, and failure rather than treating every request as immediately successful.

Keep an error boundary near the operation

If an awaited promise rejects, control behaves like an exception at that await expression. Catch an error when you can add context, recover, or show a useful message. Do not silently replace every failure with an empty result, because a network failure and a genuinely empty collection mean different things. A finally block is useful for clearing a loading state. Also consider whether a delayed response still belongs to the currently selected item before updating the screen.

javascript · Worked example
async function loadItems() {
  const response = await fetch("/items");
  if (!response.ok) throw new Error("Request failed");
  const items = await response.json();
  if (!Array.isArray(items)) throw new Error("Expected an array");
  return items;
}
// The caller handles loading, success, and failure states.

Test time as well as values

Use examples with a fast success, a slow success, and a rejection. Then change the selected record before the first request returns and observe whether an old response overwrites newer state. This exercise reveals that an asynchronous boundary is also a coordination boundary. A type checker can reject many shape mistakes, but it does not decide the correct ordering policy for your product. Make that policy explicit and test it through the behavior a learner or customer sees.

Try it yourself

A page treats a failed request as an empty list. What information is lost, and what states should it distinguish?

Show a possible solution

It loses the distinction between a successful empty result and a failure to load. Represent loading, successful data (including an empty list), and an error separately; decide how retries and stale responses are handled.

Check your understanding

Try 11 questions across javascript and typescript. Read each explanation and revisit anything that needs another pass.

Practise this subject

Go to the source

Use these primary references to check details and explore the topic further.

Saved in this browser. Signed-in progress syncs to your account.