{"success":true,"name":"guard-async-javascript-against-race-conditions","description":"JS code fires multiple async operations that touch the same data or UI.","category":"ai-brimind","path":"ai-brimind/guard-async-javascript-against-race-conditions/SKILL.md","file":"SKILL.md","content":"---\nname: guard-async-javascript-against-race-conditions\ndescription: \"JS code fires multiple async operations that touch the same data or UI.\"\n---\n\n# Guard Async JavaScript Against Race Conditions\n\n## When to use\nProject is JavaScript, and user actions or events can trigger overlapping async calls (search-as-you-type, double clicks, parallel fetches updating the same state).\n\n## Example request\nTyping fast in a search box → an older, slower response arrives last and overwrites the newest results — stale response race. Double-clicking a submit button → two orders are created — missing in-flight lock. Two fetches both read a counter then write it back → one update is lost — read-modify-write race.\n\n## Instructions\nAct like a developer who assumes every async operation can finish in any order, including the wrong one. When async calls can overlap: (1) identify shared targets — what state, DOM, or storage do these operations all write to? (2) pick a protection that fits: ignore stale responses (track a request id or use AbortController to cancel the previous fetch), lock the trigger (disable the button while a request is in flight), or serialize (queue operations so they run one at a time); (3) never assume 'requests finish in the order sent' — networks reorder freely. Before coding, narrate the bad interleaving out loud: 'request A starts, B starts, B finishes, A finishes — now what is on screen?'. If the answer is wrong data, the race is real and needs one of the guards above. Only proceed once the overlapping scenario is explicitly handled, and explain which mechanism (cancellation, locking, or queueing) closes it and why.\n\n## Metadata\n- role: developer\n- level: tactical\n- places: vibeCoding\n- origin: ai.brimind.pro skill #1009\n","linked_files":[],"origin":"ai.brimind.pro","owner_id":null,"source":"router"}