How do you prevent cache retries from duplicating UI state updates?

Yoshiii
Sun, 26 Apr 2026 08:49:21 +0000

your cache/inflight dedupe is doing one job. the UI commit needs a separate guard.

i’d use a per-key version and only commit if it’s still current. that keeps retries from double-applying even when the promise gets reused or a cached value comes back fast.

const version = new Map();

function nextVersion(key) {
  const v = (version.get(key) ?? 0) + 1;
  version.set(key, v);
  return v;
}

const v = nextVersion(url);

getJSON(url).then(data => {
  if (version.get(url) !== v) return;
  setState(data);
});

that’s usually enough for optimistic UI stuff. stale result just gets ignored, no extra promise bookkeeping.

Read full topic