Optimistic update

An optimistic update changes local application state immediately before the server confirms the operation. It makes interactions feel faster, but the application must handle failures and preserve enough information to recover safely.

Minimal example

ts
const result = useCart.getState().add(5, line);

if (result !== "ok") {
  throw new Error("Cart belongs to another restaurant");
}

Learn this concept

Lesson 4.19 · Resilient client patterns

Used in exercises

Exercise 4.21 · Cart synchronization helper
Prerequisite conceptsZustand