How to Manage Multiple AJAX Calls Efficiently: Parallel vs Serial Strategies

To improve page load speed while fetching additional data via AJAX, developers can choose between parallel and serial requests and handle concurrency using techniques such as sequential execution, callback counting, non‑blocking loops, or jQuery Deferred objects, each with its own trade‑offs.

Java High-Performance Architecture
Java High-Performance Architecture
Java High-Performance Architecture
How to Manage Multiple AJAX Calls Efficiently: Parallel vs Serial Strategies

Usually, to reduce page load time, the core content is displayed first and other data is fetched via AJAX after the page finishes loading.

This can generate multiple AJAX requests; for better user experience, parallel requests are preferred, which introduces concurrency challenges.

1. Convert Parallel to Serial

If business logic and user experience allow, converting to serial execution is the simplest solution.

function async1(){
    //do sth...
    async2();
}
function async2(){
    //do sth...
}
async1();

2. Callback Counting

Use a counter in a shared callback to determine when all asynchronous operations have completed.

function async1(){
    //do sth...
    callback();
}
function async2(){
    //do sth...
    callback();
}
function callback(){
    cnt++;
    if (2 == cnt) console.log('All completed');
}

3. Non‑Blocking Loop

Employ a timed interval to poll a counter; this method consumes more CPU, so use it judiciously.

function async1(){
    //do sth...
    cnt++;
}
function async2(){
    //do sth...
    cnt++;
}
var interval = setInterval(function(){
    if (2 === cnt){
        console.log('Execution finished');
        clearInterval(interval);
    }
}, 100);

4. jQuery Deferred

Leverage jQuery’s Deferred objects: resolve each request when finished and use $.when to run code after all are resolved.

var d1 = $.Deferred();
var d2 = $.Deferred();

function async1(){
    d1.resolve("Fish");
}
function async2(){
    d2.resolve("Pizza");
}
$.when(d1, d2).done(function(v1, v2){
    console.log(v1 + v2 + " completed");
});
Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

JavaScriptconcurrencyasync programmingajaxjQuery
Java High-Performance Architecture
Written by

Java High-Performance Architecture

Sharing Java development articles and resources, including SSM architecture and the Spring ecosystem (Spring Boot, Spring Cloud, MyBatis, Dubbo, Docker), Zookeeper, Redis, architecture design, microservices, message queues, Git, etc.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.