Skip to content

superseded-task-write ​

A run of a useTask (Vue or React) or of std's latest that changes state itself after an await, without asking its signal whether a newer run started meanwhile. On by default, as an error.

Why: a task aborts the previous run's signal when a new run starts, and drops what a superseded run returns: only the newest run's answer reaches task.state. What the callback writes itself isn't dropped. Pick a second file while the first is still being read, and the first read finishes later and opens its file over the second: the screen shows the file you didn't pick last. The callback calls nothing that takes the signal, so nothing stops it.

❌ Rejected: an older run finishes later and overwrites the newer one's

ts
import { useTask } from "@evobug_com/safe-ts-vue/task";
import { ref } from "vue";

declare function readFile(name: string): Promise<string>;

export function useViewer() {
  const open = ref("");
  const pick = useTask(async (_signal, name: string): Promise<void> => {
    open.value = await readFile(name);
  });
  return { open, pick };
}
text
This `useTask` run changes `open.value` after the `await` on line 9 without asking its signal:
when a newer run starts meanwhile, this one still finishes, and its change lands after the newer
one's
  = help: Return the value and let the task hold it (a superseded run's answer is dropped); or
    stop when superseded: `if (signal.aborted) return;` after the `await`, or hand `signal` to the
    awaited call

✅ Accepted: return the value; the task holds the newest run's (pick.state, shown with match)

✓ Accepted0 diagnostics
ts
import { useTask } from "@evobug_com/safe-ts-vue/task";

declare function readFile(name: string, init?: { signal?: AbortSignal }): Promise<string>;

export function useViewer() {
  return useTask((signal, name: string) => readFile(name, { signal }));
}

✅ Also accepted: write it yourself, after asking the signal

✓ Accepted0 diagnostics
ts
import { useTask } from "@evobug_com/safe-ts-vue/task";
import { ref } from "vue";

declare function readFile(name: string): Promise<string>;

export function useViewer() {
  const open = ref("");
  const pick = useTask(async (signal, name: string): Promise<void> => {
    const text = await readFile(name);
    if (signal.aborted) return;
    open.value = text;
  });
  return { open, pick };
}

A call given the signal (readFile(name, { signal })) rejects when a newer run aborts it, so nothing after its await runs in a superseded run: a write after it is fine too.

The same mistake one call down: the callback lends state to a function that changes it after an await of its own, and doesn't give it the signal.

❌ Rejected: open changes the viewer after it waits, and can't know the run was superseded

ts
import { type Mut, mut } from "@evobug_com/safe-ts-std/mut";
import { useTask } from "@evobug_com/safe-ts-vue/task";

declare function read(name: string): Promise<string>;

interface Viewer {
  open: string;
}

async function open(viewer: Mut<Viewer>, name: string): Promise<void> {
  viewer.open = await read(name);
}

export function usePicker(viewer: Mut<Viewer>) {
  return useTask((_signal, name: string) => open(mut(viewer), name));
}
ts
import { type Mut, mut } from "@evobug_com/safe-ts-std/mut";
import { useTask } from "@evobug_com/safe-ts-vue/task";

declare function read(name: string): Promise<string>;

interface Viewer {
  open: string;
}

async function open(viewer: Mut<Viewer>, name: string): Promise<void> {
  viewer.open = await read(name);
}

export function usePicker(viewer: Mut<Viewer>) {
  return useTask((_signal, name: string) => open(viewer, name));
}

✅ Accepted: pass the signal on, and stop on it after the await

✓ Accepted0 diagnostics
ts
import { type Mut, mut } from "@evobug_com/safe-ts-std/mut";
import { useTask } from "@evobug_com/safe-ts-vue/task";

declare function read(name: string): Promise<string>;

interface Viewer {
  open: string;
}

async function open(viewer: Mut<Viewer>, name: string, signal: AbortSignal): Promise<void> {
  const text = await read(name);
  if (signal.aborted) return;
  viewer.open = text;
}

export function usePicker(viewer: Mut<Viewer>) {
  return useTask((signal, name: string) => open(mut(viewer), name, signal));
}
ts
import { type Mut, mut } from "@evobug_com/safe-ts-std/mut";
import { useTask } from "@evobug_com/safe-ts-vue/task";

declare function read(name: string): Promise<string>;

interface Viewer {
  open: string;
}

async function open(viewer: Mut<Viewer>, name: string, signal: AbortSignal): Promise<void> {
  const text = await read(name);
  if (signal.aborted) return;
  viewer.open = text;
}

export function usePicker(viewer: Mut<Viewer>) {
  return useTask((signal, name: string) => open(viewer, name, signal));
}

What counts ​

  • The callback is the one written in place as the first argument of useTask (@evobug_com/safe-ts-vue, @evobug_com/safe-ts-react) or of latest (@evobug_com/safe-ts-std/async). Both packages and latest drop a superseded run's answer (they test the run's signal after the callback returns), so returning the value is always fine.
  • A change is an assignment, ++, delete or a changing method (push, set, splice, ...) on state that outlives the run: something the callback captured (a ref's .value, a component's state, a Mut parameter of the enclosing function), module state, this, a part of one of the callback's own arguments, a local naming one of those (const list = viewer.items); and mut(x) of such state handed to a call.
  • After an untested await: on some path, the run waited at an await that wasn't handed the signal, and didn't test the signal since. Handed means the awaited expression mentions it (fetch(url, { signal }), load(id, signal), AbortSignal.any([signal, timeout])); the rule takes such a call to reject when the signal aborts, which fetch and std's helpers do, and a project function given the signal is trusted to. A method on what such an await gave (await res.json() of a fetch given the signal) is handed it too: the body is aborted with the request. A test is reading signal.aborted (or signal.reason), signal.throwIfAborted() (also inside unsafe(() => ...)), or passing the signal to any call.
  • Lent to a function that changes it later: a call the run waits for (awaited, returned, or the arrow's body) that isn't given the signal, of a function by name (in the file, or imported) that is async and uses a parameter declared Mut<T> after its first await; or a call after an untested await to a function that declares the parameter Mut<T> at all.

How it relates to the other async rules ​

  • unused-task-signal (idiomatic) asks a callback that never reads its signal to pass it to a call that could take it: a request without it keeps running. A callback whose calls take no signal isn't its business; when such a callback changes state after an await, it is this rule's: the run has nothing to pass the signal to, and has to ask it.
  • latest-wins-counter is the hand-written form of the same question (++runs before the await, compared after) and asks for latest(...) or useTask.
  • stale-across-await needs a read before the await that the write is made from: a different mistake (a lost update), reported on different writes.

What the rule can't see ​

  • A callback passed by name (useTask(load)): only one written in place is followed.
  • A change two calls down: a function the callback lends state to, which hands it to another async function that changes it after waiting. Only the function the callback calls is read.
  • State changed through a method of an object (store.setOpen(text), viewer.open(text)): a method call is a change only when it is one of the known changing methods (push, set, ...) on the state itself.
  • A signal tested through a local (const stop = signal.aborted before the await), or a callback whose signal parameter is destructured (({ aborted }) => ...): not followed.
  • A for await loop over something given the signal: its body is taken as handed the signal for every round, whatever else it awaits.

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