Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6906279
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T08:17:22+00:00 2026-05-27T08:17:22+00:00

I’m creating a website that will feature news articles. These articles will appear in

  • 0

I’m creating a website that will feature news articles. These articles will appear in two columns at the bottom of the page. There will be a button at the bottom to load additional news stories. That means that I need to be able to specify what news story to load. Server-side, I’m simply implementing this with a LIMIT clause in my SQL statement, supplying the :first parameter like so:

SELECT * 
FROM news 
ORDER BY `date` DESC 
LIMIT :first, 1

This means that, client-side, I need to keep track of how many news items I’ve loaded. I’ve implemented this by having the function to load new information be kept in an object with a property holding the number of items loaded. I’m worried that this is somehow a race condition that I am not seeing, though, where my loadNewInformation() will be called twice before the number is incremented. My code is as follows:

var News = {

    newInfoItems: 0,

    loadNewInformation: function(side) {
        this.newInfoItems += 1;
        jQuery.get(
            '/api/news/'+ (this.newInfoItems - 1),
            function(html) {
                jQuery('div.col'+side).append(html);
            }
        );
    }
}

On page load, this is being called in the following fashion:

News.loadNewInformation('left');
News.loadNewInformation('right');

I could have implemented this in such a way that the success handler of a first call made another AJAX request for the second, which clearly would not be a race condition…but this seems like sloppy code. Thoughts?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T08:17:23+00:00Added an answer on May 27, 2026 at 8:17 am

    (Yes, there is a race condition.)

    Addressing Just the JavaScript

    All JavaScript code on a page (excluding Web-Workers), which includes callbacks, is run “mutually exclusive”.

    In this case, because newInfoItems is eagerly evaluated, it is not even that complex: both “/api/news/0” and “/api/news/1” are guaranteed to be fetched (or fail in an attempt). Compare it to this:

    // eager evaluation: value of url computed BEFORE request
    // this is same as in example (with "fixed" increment order ;-)
    // and is just to show point
    var url = "/api/news/" + this.newInfoItems
    this.newInfoItems += 1;
    jQuery.get(url, 
        function(html) {
            // only evaluated on AJAX callback - order of callbacks
            // not defined, but will still be mutually exclusive.
            jQuery('div.col'+side).append(html);
        }
    );
    

    However, the order in which the AJAX requests complete is not defined and is influenced by both the server and browser. Furthermore, as discussed below, there is no atomic context established between the server and individual AJAX requests.

    Addressing the JavaScript in Context

    Now, even though it’s established that “/api/news/0” and “/api/news/1” will be invoked, imagine this unlikely, but theoretically possible situation:

    • articles B,A exist in database
    • browser sends both AJAX requests — asynchronously or synchronously, it doesn’t matter!
    • an article is added to the database sometime between when
      • the server processes the news/0 request, and
      • the server processes the news/1 request

    Then, this happens:

    • news/0 returns article B (articles B,A in database)
    • article C added
    • news/1 returns article B (articles C,B,A in database)

    Note that article B was returned twice! Oops 🙂

    So, while the race-condition “seems fairly unlikely”, it does exist. A similar race condition (with different results) can occur if news/1 is processed before news/0 and (once again) an article is added between the requests: there no atomic guarantee in either case!

    (The above race condition would be more likely if executing the AJAX requests in-series as the time for a new article being added is increased.)

    Possible Solution

    Consider fetching say, n (2 is okay!) articles in a single request (e.g. “/api/latest/n”), and then laying out the articles as appropriate in the callback. For instance, the first half of the articles on the left and the second half on right, or whatever is appropriate.

    As well as eliminating the particular race-condition above by making the single request an atomic action — with respect to article additions — it will also result in less network traffic and less work for the server.

    The fetch for the API might then look like:

    SELECT * 
    FROM news 
    ORDER BY `date` DESC 
    LIMIT :n
    

    Happy coding.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

That's pretty much it. I'm using Nokogiri to scrape a web page what has
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I need a function that will clean a strings' special characters. I do NOT
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I used javascript for loading a picture on my website depending on which small
Basically, what I'm trying to create is a page of div tags, each has
I've got a string that has curly quotes in it. I'd like to replace
I have a French site that I want to parse, but am running into
I'm trying to create an if statement in PHP that prevents a single post
I'm working with an upstream system that sometimes sends me text destined for HTML/XML

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.