← Back to patterns

pending, ready & loading

Built-in status for queries, commands and forms, no manual flags

Pattern Usage:

const visitors = getVisitors();

visitors.ready    // false until the first value arrived, then stays true
visitors.loading  // true on first load AND during refresh()
visitors.current  // typed as T once ready is true

sendPing.pending      // number of command calls in flight
sendFeedback.pending  // number of form submissions in flight

{#if !visitors.ready}
  <Spinner />
{:else}
  <p class:stale={visitors.loading}>{visitors.current.online}</p>
{/if}

Live Demo:

Query

first load…

ready
false
loading
true

Command

Click several times quickly.

pending
0
pongs
0

Form

pending
0
submitted
false
result
–

How it works: Every remote function tracks its own status, so the let isSubmitting = $state(false) plus try/finally dance from the older demos is no longer needed.

ready tells "never loaded" apart from "refreshing", which lets you keep showing the stale value (dimmed here) instead of a spinner. It also narrows the type: after ready is true, current is no longer undefined. For commands and forms, pending is a count, so overlapping calls are handled.