<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
  <channel>
    <title>Fetch API on Nalar</title>
    <link>https://nalar.dev/tags/fetch-api/</link>
    <description>Recent content in Fetch API on Nalar</description>
    <generator>Hugo</generator>
    <language>en-us</language>
    <lastBuildDate>Tue, 01 Sep 2026 00:00:00 +0700</lastBuildDate>
    <atom:link href="https://nalar.dev/tags/fetch-api/index.xml" rel="self" type="application/rss+xml" />
    <item>
      <title>Cancel Fetch Requests and Prevent Stale UI Updates with AbortController</title>
      <link>https://nalar.dev/abortcontroller-cancel-fetch-stale-requests/</link>
      <pubDate>Tue, 01 Sep 2026 00:00:00 +0700</pubDate>
      <guid>https://nalar.dev/abortcontroller-cancel-fetch-stale-requests/</guid>
      <description>&lt;p&gt;Fast user interfaces frequently start requests that become irrelevant before they finish. A search box may issue a request for &lt;code&gt;ca&lt;/code&gt;, then &lt;code&gt;cat&lt;/code&gt;, then &lt;code&gt;catalog&lt;/code&gt;. If the oldest request finishes last, it can overwrite the newest results.&lt;/p&gt;&#xA;&lt;p&gt;&lt;code&gt;AbortController&lt;/code&gt; gives JavaScript a standard way to cancel Fetch requests and other APIs that accept an &lt;code&gt;AbortSignal&lt;/code&gt;.&lt;/p&gt;&#xA;&lt;h2 id=&#34;the-basic-cancellation-pattern&#34;&gt;The basic cancellation pattern&lt;/h2&gt;&#xA;&lt;p&gt;Create a controller and pass its signal to &lt;code&gt;fetch&lt;/code&gt;:&lt;/p&gt;&#xA;&lt;div &#xA;    x-data=&#34;{&#xA;        code: $el.querySelector(&#39;code&#39;).innerText&#xA;    }&#34;&#xA;    class=&#34;relative my-4 rounded-lg group&#34;&#xA;&gt;&#xA;    &lt;button&#xA;        @click=&#34;navigator.clipboard.writeText(code); $el.innerText = &#39;Copied!&#39;; setTimeout(() =&gt; $el.innerText = &#39;Copy&#39;, 2000)&#34;&#xA;        class=&#34;absolute top-2 right-2 bg-neutral-700 text-white text-xs px-2 py-1 rounded opacity-0 group-hover:opacity-100 transition-opacity&#34;&#xA;    &gt;&#xA;        Copy&#xA;    &lt;/button&gt;&#xA;    &#xA;    &lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-js&#34; data-lang=&#34;js&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#66d9ef&#34;&gt;const&lt;/span&gt; &lt;span style=&#34;color:#a6e22e&#34;&gt;controller&lt;/span&gt; &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#66d9ef&#34;&gt;new&lt;/span&gt; &lt;span style=&#34;color:#a6e22e&#34;&gt;AbortController&lt;/span&gt;();&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#66d9ef&#34;&gt;const&lt;/span&gt; &lt;span style=&#34;color:#a6e22e&#34;&gt;request&lt;/span&gt; &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#a6e22e&#34;&gt;fetch&lt;/span&gt;(&lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#39;/api/profile&amp;#39;&lt;/span&gt;, {&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#a6e22e&#34;&gt;signal&lt;/span&gt;&lt;span style=&#34;color:#f92672&#34;&gt;:&lt;/span&gt; &lt;span style=&#34;color:#a6e22e&#34;&gt;controller&lt;/span&gt;.&lt;span style=&#34;color:#a6e22e&#34;&gt;signal&lt;/span&gt;,&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;});&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#a6e22e&#34;&gt;controller&lt;/span&gt;.&lt;span style=&#34;color:#a6e22e&#34;&gt;abort&lt;/span&gt;();&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&#xA;&lt;/div&gt;&#xA;&lt;p&gt;Once aborted, the fetch promise rejects. Cancellation is therefore part of normal control flow and should be handled deliberately.&lt;/p&gt;</description>
    </item>
  </channel>
</rss>
