Skip to content

vue-async-race ​

An async watcher that changes state after an await, without cancelling the run before it.

Why: the watcher runs again every time its source changes, and each run waits on its own. Type "ab" quickly and two searches are in flight; if the one for "a" is slower, it finishes last and its results replace those for "ab". Nothing crashes: the page just shows the wrong thing. Guide: Vue.

✕ Rejectedvue-async-race
ts
import { shallowRef, watch, type Ref } from "vue";

async function search(query: string): Promise<readonly string[]> {
  return query === "" ? [] : [query];
}

export function useSearch(query: Ref<string>) {
  const results = shallowRef<readonly string[]>([]);
  watch(query, async (q) => {
    const found = await search(q);
    results.value = found;
  });
  return results;
}

✅ Accepted: useTask aborts the previous run, and keeps the outcome in one value

✓ Accepted0 diagnostics
ts
import { watch, type Ref, type ShallowRef } from "vue";
import type { Enum } from "@evobug_com/safe-ts-std/enum";
import type { Result } from "@evobug_com/safe-ts-std/result";
import type { AsyncState } from "@evobug_com/safe-ts-vue";
import { useTask } from "@evobug_com/safe-ts-vue/task";

type SearchError = Enum<{ Offline: {}; Refused: { status: number } }>;

declare function search(query: string, signal: AbortSignal): Promise<Result<readonly string[], SearchError>>;

export function useSearch(query: Ref<string>): Readonly<ShallowRef<AsyncState<readonly string[], SearchError>>> {
  const results = useTask((signal, q: string) => search(q, signal));
  watch(query, (q) => results.run(q), { immediate: true });
  return results.state;
}

Each run aborts the previous run's signal, only the latest run writes state, and the task is aborted when the component that called useSearch unmounts. Show state with <Match>.

✅ Also accepted: abort the previous run by hand, when the result isn't kept as an AsyncState

✓ Accepted0 diagnostics
ts
import { onWatcherCleanup, shallowRef, watch, type Ref } from "vue";

async function search(query: string, signal: AbortSignal): Promise<readonly string[]> {
  return signal.aborted || query === "" ? [] : [query];
}

export function useSearch(query: Ref<string>) {
  const results = shallowRef<readonly string[]>([]);
  watch(query, async (q) => {
    const controller = new AbortController();
    onWatcherCleanup(() => controller.abort());
    const found = await search(q, controller.signal);
    if (!controller.signal.aborted) results.value = found;
  });
  return results;
}

Vue runs the cleanup just before the next run.

When the work can't be aborted, check after the await that this run is still the latest, and write only then. Compare a value the run started with (the callback's parameter, a const taken before the await) with the current one:

✅ Also accepted: a stale result is dropped

✓ Accepted0 diagnostics
ts
import { shallowRef, watch, type Ref } from "vue";

async function render(markdown: string): Promise<string> {
  return markdown;
}

export function useRendered(markdown: Ref<string>) {
  const html = shallowRef("");
  watch(markdown, async (text) => {
    const rendered = await render(text);
    if (markdown.value === text) html.value = rendered;
  });
  return html;
}

These count as cancelling: onWatcherCleanup(...), the cleanup parameter of the callback ((value, old, onCleanup) => ...), an AbortController or signal used in the callback, and a check after the await that compares what the run holds with the current value: the source (if (props.body === body) ...), or a run counter (const run = ++runs; before, if (run !== runs) return; after). A flag set in onWatcherCleanup (if (!cancelled) ...) works too. Changing state that the callback created itself is fine.

Writes are assignments (results.value = ..., state.field = ...) and changing methods (results.value.push(...)).

This rule is about an answer that arrives late, whatever the run read. A value read from shared state before the await and written back from it (a lost update), or a check made before it and acted on after it, is stale-across-await's: the two report different writes.

What the rule can't see: a write made by another function the callback calls after the await.

MIT OR Apache-2.0 · Docs by Gemini 4 Neon Extra