latest-wins-counter
A let n = 0 outside an async function that the function increments before an await and compares after it, whatever it is called: a hand-written "latest run wins" ticket. An idiomatic rule: reported only with "safe-ts": { "idiomatic": true } in package.json.
Why: the ticket throws away an older answer, but the older request keeps running, and the ticket lives next to two or three refs that must agree with it. useTask is the same idea in one place: each run(...) aborts the previous run's signal, only the newest run writes its state, and the task is aborted when its scope ends. That scope can be a component's setup or a Pinia setup store. Guide: Async state. Outside a framework (plain DOM code, a CLI, a worker), latest(...) from @evobug_com/safe-ts-std/async is the same without the state: each run(...) aborts the previous run's signal, and an older run resolves to err(Superseded). The message names latest in a file that imports neither Vue nor React, and useTask in one that does. Guide: Async code.
✕ Rejectedlatest-wins-counter
ts
import type { Enum } from "@evobug_com/safe-ts-std/enum";
import { type Result, isErr } from "@evobug_com/safe-ts-std/result";
import { type Ref, ref } from "vue";
type RequestError = Enum<{ Offline: {}; Status: { status: number } }>;
declare function fetchTeam(id: string): Promise<Result<string[], RequestError>>;
export function useTeam(): { readonly members: Ref<string[]>; readonly ask: (id: string) => Promise<void> } {
const members = ref<string[]>([]);
let latestAsk = 0;
async function ask(id: string): Promise<void> {
const mine = ++latestAsk;
const found = await fetchTeam(id);
if (mine !== latestAsk || isErr(found)) return;
members.value = found;
}
return { members, ask };
}✓ Accepted0 diagnostics
ts
import type { Enum } from "@evobug_com/safe-ts-std/enum";
import type { Result } from "@evobug_com/safe-ts-std/result";
import { useTask } from "@evobug_com/safe-ts-vue/task";
type RequestError = Enum<{ Offline: {}; Status: { status: number } }>;
declare function fetchTeam(id: string, signal: AbortSignal): Promise<Result<string[], RequestError>>;
export function useTeam() {
return useTask((signal, id: string) => fetchTeam(id, signal));
}✅ Accepted: without a framework
✓ Accepted0 diagnostics
ts
import { latest, Superseded } from "@evobug_com/safe-ts-std/async";
import type { Enum } from "@evobug_com/safe-ts-std/enum";
import { isErr, type Result } from "@evobug_com/safe-ts-std/result";
type RequestError = Enum<{ Offline: {}; Status: { status: number } }>;
declare function fetchTeam(id: string, signal: AbortSignal): Promise<Result<string[], RequestError>>;
export function teamPicker(show: (members: string[]) => void, page: AbortSignal): (id: string) => Promise<void> {
const team = latest((signal, id: string) => fetchTeam(id, signal), page);
return async (id) => {
const members = await team.run(id);
if (isErr(members)) {
if (!(members.error instanceof Superseded)) console.warn(members.error);
return;
}
show(members);
};
}A counter that only counts (calls++, read in a message) or is tested before the first await isn't reported. Not followed: a counter kept in a class field or an object (this.seq++).
The ticket's own writes are made before the await, so stale-across-await says nothing about them. That rule reports state read before an await and written back from what was read (or checked before it and changed after it), which a ticket doesn't fix: latest(...) and useTask drop an older answer, not an update another run made in between.