Skip to content

stale-across-await ​

Shared state read before an await and written after it from that read: a lost update (this.total += await price(id)), or a check made before the await and acted on after it (a cache that two callers both miss, a "stopping" flag set only after the work). On by default, as a warning.

Why: JavaScript runs one thing at a time, so between two awaits nothing else touches your state; across one, anything can. this.total += await price(id) reads this.total first (that is JavaScript's order), then waits: a second add that runs meanwhile reads the same old total, and whichever finishes last overwrites the other. Rust refuses the equivalent (a reference into shared state can't be held across the point where someone else may change it); TypeScript sees nothing wrong. Guide: Async code.

❌ Rejected: two adds at once, one of them is lost

✕ Rejectedstale-across-await
ts
async function price(id: string): Promise<number> {
  await Promise.resolve();
  return id.length;
}

export class Cart {
  total = 0;
  async add(id: string): Promise<void> {
    this.total += await price(id);
  }
}

✅ Accepted: await first, then read and write together

✓ Accepted0 diagnostics
ts
async function price(id: string): Promise<number> {
  await Promise.resolve();
  return id.length;
}

export class Cart {
  total = 0;
  async add(id: string): Promise<void> {
    const amount = await price(id);
    this.total += amount;
  }
}

The quick fix Await first writes this for x += await f(). Between the await and the write nothing else runs, so the read and the write see the same value.

❌ Rejected: checked before the await, filled after it: two callers both miss, both load

✕ Rejectedstale-across-await
ts
import type { Option } from "@evobug_com/safe-ts-std/result";

async function load(id: string): Promise<number> {
  await Promise.resolve();
  return id.length;
}

export class Prices {
  private readonly known = new Map<string, number>();
  async get(id: string): Promise<number> {
    const hit: Option<number> = this.known.get(id);
    if (hit !== undefined) return hit;
    const value = await load(id);
    this.known.set(id, value);
    return value;
  }
}

✅ Accepted: keep the work in flight where the check looks, before the first await

✓ Accepted0 diagnostics
ts
async function load(id: string): Promise<number> {
  await Promise.resolve();
  return id.length;
}

export class Prices {
  private readonly loading = new Map<string, Promise<number>>();
  get(id: string): Promise<number> {
    const inFlight = this.loading.get(id);
    if (inFlight !== undefined) return inFlight;
    const value = load(id);
    this.loading.set(id, value);
    return value;
  }
}

The second caller finds the first one's promise and awaits the same load. The same holds for a flag: test it and set it before the first await (if (this.stopping) return; this.stopping = true;), not after.

✅ Accepted: a guard tested and set before the first await

✓ Accepted0 diagnostics
ts
async function drain(): Promise<void> {
  await Promise.resolve();
}

export class Pool {
  private stopping = false;
  stopped = false;
  async stop(): Promise<void> {
    if (this.stopping) return;
    this.stopping = true;
    await drain();
    this.stopped = true;
  }
}

What counts ​

  • Shared state is what another call can reach while this one waits: a field of this in a method (also in an arrow inside it), module state, and a local of an enclosing function that an async function captures (ids.map(async (id) => { ... total = ... })), unless that function is called right where it is written ((async () => { ... })()). A function's own locals and its parameters aren't. A parameter declared Mut<T> isn't either: the loan is this call's alone while it runs, so a write of the call's own answer to it is the call's to make. That is aliased-mut's to make true: a function that writes its Mut parameter after an await (whatever the value is made from) holds the loan across it, and every call site the checker sees that lends the object again, or writes it, while such a call is still running is reported there (in a loop, on a branch, from a listener, through a list or a field, by a function that returns with the loan still alive). Who calls a function the code doesn't show is a gap of both rules.
  • A stale write is a write to a place (this.total, cache.keys, a captured let: an assignment, ++, a changing method such as set, push, delete) on a path where the place, or a part of it (this.items.length for this.items.push(...)), was read before the last await and not read again since, and either
    • the read was the test of a guard clause (if (...) return;, also through a local read from the place: const hit = this.known.get(id); if (hit !== undefined) return hit;): check, then act; or
    • the written value is made from the read (this.total = before + p with const before = this.total, x += ...): a lost update; or the position written is (const at = this.items.length; await save(); this.items[at] = value, splice(at, ...), map.set(keyFromRead, ...)): two callers write the same slot. A value that is only this call's own answer (this.detail = await api.get(...)) isn't reported: the last call to finish wins, which is "latest run wins"'s question, not this one.
  • Fine: reading again after the await; awaiting before the read; a guard tested and set before the first await (a flag, an in-flight promise kept in a map): while it is held, a second caller doesn't get past the test, so an await doesn't make what was read stale. Writing the guard after the await releases it.

How it relates to the other async rules ​

  • vue-async-race and react-async-race are about a write after an await in a watcher or an effect whose earlier run may finish last: the answer is stale, whatever was read. This rule needs a read before the await that the write is made from (or checked by), so the two report different writes; neither replaces the other.
  • latest-wins-counter reports a hand-written ticket (++runs before, compared after) and asks for latest(...) or useTask; the ticket's own writes are made before the await, so this rule says nothing about them.
  • stale-read is the same question across a call instead of an await: a local that still holds an object a call replaced.

What the rule can't see ​

  • Who calls the function. A method only ever awaited in sequence by one caller is reported like one two callers run at once: the code of the function doesn't say.
  • State reached another way: through a local alias (const cart = this.cart; ... cart.total = ...), an element (this.items[i]), a destructuring (const { total } = this), a method that writes for you (this.setTotal(before + p)).
  • What a guard protects. A guard held on one place counts for every place the function reads; a lock or queue the project writes as a class of its own isn't recognised.
  • A Mut parameter where a caller isn't seen: the exclusivity this rule assumes for it holds at the call sites aliased-mut sees; its page lists what it doesn't (a callback the checker doesn't know runs again, a promise kept where it loses it).

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