Hey everyone, I’m wiring up an optimistic “like” button in a small pixel-art gallery app and I’m trying to keep it snappy even on flaky Wi‑Fi, but I keep seeing a failure mode where the UI briefly shows the right state and then flips back after a retry resolves.
What’s a solid pattern to prevent stale retries or out-of-order responses from overwriting the newest optimistic state without making the code way more complex?
Your retry calls toggleLike(id) again, so you’re creating a brand-new toggle, and then an older in-flight response can come back later and stomp whatever the newest optimistic state is.
Keep a per-item opId/version and only commit a response if it matches the latest one in the cache, and make the retry re-send the same op instead of flipping liked again.
Yeah, your retry is re-running the optimistic toggle, so you flip twice and a slower earlier response can land afterward and overwrite the newest state.
Give each item an opId/version, retry the same op, and ignore any response that doesn’t match the latest opId in cache.
Make the optimistic update set liked=true/false instead of toggling, and tag each request with a requestId so only the newest response is allowed to write back.
Also consider canceling or ignoring in - flight requests when a new toggle happens (AbortController in fetch, cancellation tokens elsewhere), so only the latest intent can win and older retries can’t “undo” your optimistic state.