Skip to content
  • Home
  • About
  • No Access
  • Privacy Policy
Bits of .NET
Bits of .NET
  • Asp.Net Core
  • C#
  • SQL
  • JavaScript
  • CSS
  • About
  • ErcanOPAK.com
  • No Access
  • Privacy Policy

Bits of .NET

Daily micro-tips for C#, SQL, performance, and scalable backend engineering.

  • Asp.Net Core
  • C#
  • SQL
  • JavaScript
  • CSS
  • About
  • ErcanOPAK.com
  • No Access
  • Privacy Policy
Ajax

Ajax: Fix a Request That Overwrites Newer Data With an Older Response That Arrived Late

- 31.08.26 - ErcanOPAK

🔄 The Newest Request Isn’t Always the One That Finishes Last

A user types quickly into a live-search box, firing off a request on every keystroke. The requests don’t always come back in the order they were sent – and when an older, slower response arrives AFTER a newer, faster one, it silently overwrites the correct, up-to-date results with stale ones.

🐞 The Problem

searchInput.addEventListener('input', () => {
  fetch(`/api/search?q=${searchInput.value}`)
    .then(res => res.json())
    .then(results => renderResults(results));
    // Whichever request happens to FINISH last wins,
    // regardless of which one was actually sent last.
});

🔍 Why This Happens

Each keystroke fires an independent network request, and network
timing is not guaranteed to preserve send order - a request for
"ca" might take longer than a later request for "cat" if the
server load or network conditions vary even slightly between them.
Whichever .then() callback happens to run LAST simply overwrites
the DOM with whatever it has, with no awareness of whether a newer
request is still in flight or already finished first.

✅ The Fix: Track and Ignore Stale Responses

  • Tag each request with an incrementing counter (or the query itself) before sending it, and when a response comes back, only render it if it matches the MOST RECENT request sent — discard anything older.
  • Where supported, use AbortController to actively cancel the previous in-flight request the moment a new one starts, so a stale response never even has the chance to arrive and race.
  • Add a short debounce (150-300ms) on the input event itself so a fast typist doesn’t fire a request per keystroke in the first place — this reduces how often the race condition can even occur.

📦 Correct Version (Request Tracking)

let currentRequestId = 0;

searchInput.addEventListener('input', () => {
  const thisRequestId = ++currentRequestId;
  fetch(`/api/search?q=${searchInput.value}`)
    .then(res => res.json())
    .then(results => {
      if (thisRequestId === currentRequestId) {
        renderResults(results);   // only render the LATEST request
      }
      // otherwise: a newer request has since been sent, discard this
    });
});

The bug was never about a slow response — it was about trusting arrival order to match send order, which the network never actually promised.

— Frontend Engineer

Related posts:

Ajax: Use Query Strings to Pass Data in GET Requests

Ajax: Use Query Strings to Pass Data in GET Requests

Why Your Ajax Requests “Randomly” Fail in Production

Why Your Ajax Requests “Randomly” Fail in Production

AJAX: Implementing Pagination in REST APIs

AJAX: Implementing Pagination in REST APIs

Views: 14

Post navigation

The AI Prompt That Plans a Realistic Moving-Day Checklist Down to the Hour
Docker: Fix a Multi-Stage Build That Copies Files From the Wrong Stage

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

November 2026
M T W T F S S
 1
2345678
9101112131415
16171819202122
23242526272829
30  
« Oct    

Most Viewed Posts

  • How to make theater mode the default for Youtube (1,068)
  • Get the User Name and Domain Name from an Email Address in SQL (990)
  • How to add default value for Entity Framework migrations for DateTime and Bool (986)
  • Get the First and Last Word from a String or Sentence in SQL (865)
  • How to select distinct rows in a datatable in C# (859)
  • How to enable, disable and check if Service Broker is enabled on a database in SQL Server (661)
  • Add Constraint to SQL Table to ensure email contains @ (619)
  • Average of all values in a column that are not zero in SQL (577)
  • How to use Map Mode for Vertical Scroll Mode in Visual Studio (553)
  • Find numbers with more than two decimal places in SQL (481)

Recent Posts

  • Network Speed Monitor Windows Application v2.5.1
  • Ağ Hızı İzleyici Windows Uygulaması v2.5.1
  • How to make theater mode the default for Youtube
  • The AI Prompt That Audits a Function for Thread-Safety Gaps
  • Ajax: Fix a Fetch Request Caching a Response Despite No-Store
  • WordPress: Fix a Navigation Block Losing Submenu Items After Sync
  • Visual Studio: Fix CodeLens Reference Counts Stuck at Stale Numbers
  • C#: Fix a UTF-8 String Literal Behaving Differently in a Switch
  • Photoshop: Fix a Liquify Mesh Snapping Back on a Smart Object
  • The AI Prompt That Turns a Crash Dump Into a Short Repro Script

Most Viewed Posts

  • How to make theater mode the default for Youtube (1,068)
  • Get the User Name and Domain Name from an Email Address in SQL (990)
  • How to add default value for Entity Framework migrations for DateTime and Bool (986)
  • Get the First and Last Word from a String or Sentence in SQL (865)
  • How to select distinct rows in a datatable in C# (859)

Recent Posts

  • Network Speed Monitor Windows Application v2.5.1
  • Ağ Hızı İzleyici Windows Uygulaması v2.5.1
  • How to make theater mode the default for Youtube
  • The AI Prompt That Audits a Function for Thread-Safety Gaps
  • Ajax: Fix a Fetch Request Caching a Response Despite No-Store

Social

  • ErcanOPAK.com
  • GoodReads
  • LetterBoxD
  • Linkedin
  • The Blog
  • Twitter
© 2026 Bits of .NET | Built with Xblog Plus free WordPress theme by wpthemespace.com