Invental/Review lessons/Client state that goes stale

Client state that goes stale

Late async results must not overwrite newer ones

An async result must prove it is still wanted before it writes: every path that changes the same field has to invalidate in-flight work, not just the next upload.

01 · Before and afterThe pattern

TypeScript
const gen = useRef(0);
const invalidate = () => { gen.current++; setBusy(false); };
async function onFile(file: File) {
  const mine = ++gen.current; setBusy(true);
  const value = await process(file);          // slow
  if (mine !== gen.current) return;           // something newer happened
  form.setValue("logo", value); setBusy(false);
}
// call invalidate() from: manual edit, clear, dialog close, form reset

02 · Run it on your appThe check

If a user changes their mind while an upload is still processing, can the old upload ever overwrite the new choice?

If the honest answer is no or not sure, this is worth a look before it reaches production.

Want this checked on your code?

Tell us what you are building and what worries you. We match a senior reviewer to your code and confirm scope before anything starts.

— Get in touch
hi@invental.co ↗
— Or
— What to include

A link or short description of the code or system, what you want checked, your stack, and when you need the answer. No repository access is needed until scope is agreed.

— Or leave a note
We reply within one business day.