Skip to main content

Practice ยท 1 of 2

Async UI State Machine

Write createViewState() โ€” a tiny state machine for an async view. It RETURNS { get, load }: - state starts as { status: "idle" } - load(fetchFn) sets { status: "loading" }, awaits fetchFn() - success โ†’ { status: "success", data } - failure โ†’ { status: "error", message } (err.message) - get() returns the CURRENT state object (a new object each call is fine) - calling load again while one is in flight is allowed โ€” the LAST call wins (stale completions must not overwrite the newer state)

Difficulty: intermediate

Back to lesson: Practice: Mini Build: API-Powered Widget