What is the best way to asynchronously send multiple AJAX requests and combine their results into an array?

My task involves retrieving 100 objects through an ajax request by passing an ID. However, I can only send a maximum of 20 IDs at a time. As a solution, I am sending them in batches of 20 until all the data is received from the server. What would be the most efficient way to accomplish this asynchronously?

To test this process, I have set up a global array. After receiving data from each request, I merge it with the existing array:

window['p'] = [];

function getAllProducts(products) {
    var counter = 0;
    while(counter < products.length){
        (function(counter){
            var request = new XMLHttpRequest();
            var url = '/api/catalog_system/pub/products/search?';

            // Concatenate all the IDs with the URL
            if (counter === 0){
                url += 'fq=skuId:'+products[0].sku
                for (var i = 1; i < 20; i++) {
                    url+= '&fq=skuId:'+products[i].sku;
                }
            } else {
                for (var i = counter; i < counter + 20; i++) {
                    if (typeof products[i] === 'undefined') {
                        break;
                    }
                    url+= '&fq=skuId:'+products[i].sku;
                }
            }

            request.open('get',url,true);

            request.onreadystatechange = function(){
                if (request.status == 200 || request.status == 206 && request.readyState == '4') {
                    var data = JSON.parse(request.responseText);
                    console.log(data);
                    window['p'].push(data);
                }
            }

            request.send(null);
            console.log(request);
        }(counter))
        counter+=20;
    }
}

getAllProducts(productArray);

Answer №1

Welcome back! :D Here is a cleaner approach using jQuery for better code organization and the convenience it provides. Of course, you can go the native way if you prefer.

To begin, create groups of 20 IDs for easier iteration. Then loop through each group to send an AJAX request. Using jQuery makes the process cleaner and allows working with promises simultaneously.

let products = [{sku: '151231'}, {sku: '151231'}, ...]; // array of your products
let ajaxCallData = [''];
let counter = 0;
let promises = [];
let finalDataTable = [];

// Construct a query string for each group of 20 SKUs
for (var i=0; i < products.length-1; i++) {
    if (i % 20 == 0 && i > 0) {
        ajaxCallData[counter] = '' + ajaxCallData[counter];
        ajaxCallData[++counter] = '';
    }
    ajaxCallData[counter] += '&fq=skuId:' + products[i].sku;
}

// Push promises into the array
$.each(ajaxCallData, function(index) {
    promises.push($.get('/api/catalog_system/pub/products/search?' + ajaxCallData[index]));
});

// Resolve all promises when ready
Promise.all(promises).then(function(values) {
    // Gather resolved requests in original order
    if (values && values.length)
        values.forEach(x => finalDataTable.push(x));

}).catch(function(info){
    // Handle errors here
});

You can view a demo on this fiddle link, although not fully functional, it can serve as a starting point for implementing this solution in your project.

Fiddle Demo

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Showing JSON data on a webpage

Hey there, I'm looking to showcase information from a MySQL table in an HTML document. Currently, I have a PHP script that is up and running: <html> <head> </head> <body> <?php $mysqli = new mysq ...

Separate an undetermined length numpy 2D array into a 3D array

I am working with a numpy array of dimensions (18, 10525). It consists of 18 columns and 10525 rows; however, the number of rows may vary, making it necessary to slice the array into groups or windows of 200 rows for AI processing. For instance, I intend ...

Utilizing the handleChange method to manage multiple input fields

It is important to me that the function handleChange only updates the input value that is being changed. Specifically, if I modify the 'firstName' input, I want only that state to be updated, and the same applies to the 'lastName' input ...

Several queries for directions on Google Maps are resulting in the same response being returned for all requests

I've been experimenting with the Google Maps Directions service to retrieve three different travel methods for the same route (driving, walking, and bicycling). Despite successfully iterating through the array of methods and setting them in the respec ...

It appears that the slice function is not functioning correctly within an array of functions

My code includes a function called UIAlerts: function UIAlerts() {} I then use the prototype methodology to assign a variable to this function: UIAlerts.prototype.questions = [] Next, I add a function to this array: UIAlerts.prototype.questions.push(f ...

format.js or format.html: Understanding the Differences in Rails

I am looking to incorporate AJAX to load a partial in my create action. Most tutorials suggest using the line format.js within the respond_to block and creating a create.js.erb file. However, I have some code in my create action which needs to be executed. ...

Creating and dynamically updating identical state using a single method in React

Is there a way to create a single method named handleChange that can handle checkbox changes and update their respective states appropriately? I attempted to dynamically call a function based on the checkbox name, but it's not working as expected. Can ...

Why Are My JavaScript GET Request Parameters Displaying as Strings Rather Than Numbers?

Currently, I am in the process of developing a REST API where one of the defined routes looks like this: router.get("/objects/:id?/:val1?/:val2?", getObject); Specifically, my request from Postman appears as follows: http://localhost:8000/objects?val1= ...

Transferring data to server using AngularJS and Java Servlet Technology

I am currently facing a challenge in developing a webpage that enables file uploads from a local machine to a server using AngularJS and Java Servlet. My approach involves sending data to the server using $http.post and attempting to read the file data usi ...

Send data to local REST service with Jquery/Ajax for file upload

I am currently trying to implement a REST service that allows me to upload files to our database using the file explorer. So far, I have successfully managed to open the file explorer by clicking on my findDocumentOnboarding button within an input type="fi ...

How to effectively create factories in AngularJS

I stumbled upon this angularjs styleguide that I want to follow: https://github.com/johnpapa/angular-styleguide#factories Now, I'm trying to write my code in a similar way. Here is my current factory implementation: .factory('Noty',functi ...

Adding an additional stroke to a Material-UI Circular Progress component involves customizing the styling properties

I am attempting to create a circular progress bar with a determinate value using Material-UI, similar to the example shown in this circular progress image. However, the following code does not display the additional circle as the background: <CircularP ...

Developing a Link Element in Angular 2

Exploring the creation of a wrapper component in Angular 2, inspired by tools like react-bootstrap. The goal is to avoid repeating the component structure each time and instead create a reusable component. The desired reusable component should have a stru ...

Bootstrap dropdown fails to function following an AJAX request, despite attempting to reinitialize it

After doing some research on stackoverflow, I learned that it's important to refresh the dropdown on an ajax .load() event. However, I am a bit puzzled because the dropdown stops working after clicking it a couple of times. :/ jquery $(document).rea ...

Error in Django: Mobile AJAX request is returning 403 forbidden despite CSRF token being set

Testing my website on mobile platforms has been a challenge while using Django. The server is configured to load posts continuously using AJAX from a Django view as the user scrolls. Running the server from the command line and testing on a desktop browse ...

Tips for transferring data from a form to datatables

Currently, I am experimenting with Laravel and Datatables. If you click here, you will find a table with filtering options that interest me. I have already set up the routes and controllers as shown in the example, but I am facing an issue with dynamically ...

The DOM HTMLElement's className property is empty when the element does not have a class name specified

What is the value of the HTMLElement className property when it has no class name set in HTML? Initially, I thought it would be undefined, but I discovered that in Firefox, it is actually an empty string. This raises the question - can this behavior be r ...

Displaying model errors in JSON format within a JavaScript template

When submitting a form with the data-remote=true attribute, the controller's create action is defined as: format.js { @user } This action is then interpreted by the template called: create.js.erb Is there a simple way to display the @user.errors m ...

When trying to sign up a user in Nuxt, an error occurs stating "Cannot read property 'then' of undefined."

I've been encountering an issue while trying to sign up or log in as a user from my Nuxt app to an API that is functioning perfectly. The error message Cannot read property 'then' of undefined keeps popping up, and I can't figure out th ...

Using jQuery to organize object properties based on the value of another property

Within my collection, I have an array of objects structured as shown below. [ {"rel_id": 1,"forward_flag": true,"asset_id":5,}, {"rel_id": 1,"forward_flag": true,"asset_id":8}, {"rel_id": 1,"forward_flag": false,"asset_id":3}, {"rel_id": 2,"forwar ...