{"id":1272,"date":"2025-05-22T12:31:56","date_gmt":"2025-05-22T12:31:56","guid":{"rendered":"https:\/\/www.cmarix.com\/qanda\/?p=1272"},"modified":"2026-02-05T12:06:03","modified_gmt":"2026-02-05T12:06:03","slug":"how-do-you-handle-race-conditions-in-api-calls","status":"publish","type":"post","link":"https:\/\/www.cmarix.com\/qanda\/how-do-you-handle-race-conditions-in-api-calls\/","title":{"rendered":"How do you Handle Race Conditions in API Calls?"},"content":{"rendered":"\n<p>Race conditions can occur when multiple API calls are made in quick succession. One such instance can be, when a user types rapidly into a search field. If earlier requests return after newer ones, outdated data can overwrite the latest results. We can prevent this by using the AbortController API to cancel in-flight requests before starting a new one. This ensures only the most relevant response updates the state.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code> useEffect(() => {\n  const controller = new AbortController();\n  const fetchData = async () => {\n    try {\n      const response = await fetch(`\/api\/data?query=${query}`, {\n        signal: controller.signal,\n      });\n      const result = await response.json();\n      setData(result);\n    } catch (err) {\n      if (err.name !== 'AbortError') {\n        console.error('API Error:', err);\n      }\n    }\n  };\n  fetchData();\n  return () => {\n    controller.abort(); \/\/ Cancels the previous request\n  };\n}, &#91;query]);\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Race conditions can occur when multiple API calls are made in quick succession. One such instance can be, when a user types rapidly into a search field. If earlier requests return after newer ones, outdated data can overwrite the latest results. We can prevent this by using the AbortController API to cancel in-flight requests before [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":1277,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[6,3],"tags":[],"class_list":["post-1272","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-react","category-web"],"acf":[],"_links":{"self":[{"href":"https:\/\/www.cmarix.com\/qanda\/wp-json\/wp\/v2\/posts\/1272","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.cmarix.com\/qanda\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.cmarix.com\/qanda\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.cmarix.com\/qanda\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.cmarix.com\/qanda\/wp-json\/wp\/v2\/comments?post=1272"}],"version-history":[{"count":6,"href":"https:\/\/www.cmarix.com\/qanda\/wp-json\/wp\/v2\/posts\/1272\/revisions"}],"predecessor-version":[{"id":1280,"href":"https:\/\/www.cmarix.com\/qanda\/wp-json\/wp\/v2\/posts\/1272\/revisions\/1280"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.cmarix.com\/qanda\/wp-json\/wp\/v2\/media\/1277"}],"wp:attachment":[{"href":"https:\/\/www.cmarix.com\/qanda\/wp-json\/wp\/v2\/media?parent=1272"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.cmarix.com\/qanda\/wp-json\/wp\/v2\/categories?post=1272"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.cmarix.com\/qanda\/wp-json\/wp\/v2\/tags?post=1272"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}