How to send Javascript/jQuery AJAX POST requests sequentially, looping over an array of request data, using Promises?

Anto Jose

I have a huge list of articles, visually indicated by a table listing of article titles, on which we have to perform a particular processing action, processing items one after the other, by sending a POST request with the id of that article, while removing that row from the table listing, as a visual indicator of the progress.

This is what I used originally:

var article_ids = [1,2,...];
article_ids.each(function (value, index) {
  var id = value;
  jQuery.ajax({
    type: "POST",
    url: "index.php?process_article",
    data: {article_id: id},
    cache: false,
    async: false
  })
  .done(function(reponse) {
    console.log(response);
    jQuery('.article-id-' + id).css('display', 'none');
  });  
});

And it works fine in FireFox, processing items one by one, removing corresponding rows from the table listing.

But in Google Chrome, all of these seem to be processed in one go, instead of these requests happening one after the completion of the other.

After a bit of googling, found references saying to use Javascript Promises, which I couldn't figure out how to use for this case.

And then, instead, I changed the above, to the following recursive function, which seems to work fine on both Chrome and Firefox, with requests happening one after the other:

var article_ids = [1,2,...];
function processIds(ids) {
  if (ids.length > 0) {
    id = ids.shift();     
    console.log("Processing ID: " + id);
    jQuery.ajax({
      type: "POST",
      url: "index.php?process_article",
      data: {article_id: id},
      cache: false
    })
    .done(function() {
      jQuery('.article-id-' + id).css('display', 'none');
      processIds(ids);
    });
  } else {
    alert("Successfully processed all articles.");
  }
}
processIds(article_ids);

DOUBTS:

  1. Is this a good way to go about this?
  2. How could we achieve the same using Javascript Promises?
Rajit

The recursive solution you've suggested works and makes sense, IMO. In this situation I usually use Array.reduce, though.

Here's an example:

function processIds(ids) {
  return ids.reduce((promise, nextId) => {
    return promise.then(() =>
      jQuery.ajax({
        type: "POST",
        url: "index.php?process_article",
        data: { article_id: nextId },
        cache: false,
      })
    );
  }, Promise.resolve());
}

Note that the return values contained in the promises are thrown away in this example and only the very last value will be retained. It would be possible to modify this to capture and aggregate all return values if you desired.

It's also easy to extract this into something reusable. For example:

function forEachAsync(myArray, funcPromise) {
  return myArray.reduce((promise, nextValue) => {
    return promise.then(() => funcPromise(nextValue));
  }, Promise.resolve());
}

And then you could use your new forEachAsync function as follows:

function processIds(ids) {
  return forEachAsync(ids, processArticle);
}

function processArticle(id) {
  return jQuery.ajax({
    type: "POST",
    url: "index.php?process_article",
    data: { article_id: nextId },
    cache: false,
  });
}

I believe the latest versions of jQuery use promises that are compatible with JavaScript promises, but this is something you should check and be aware of.

I believe they were introduced in ES2015 and jQuery introduced compatibility in v3.0.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to send a post request with data using python requests module?

Send http post request with unicoded data using python requests

How to send array of data in post request in flutter

using requests.post to send a request with a certificate

Send POST request over Mobile Data

How to send post data with ajax request though Datatable

How to add http headers along with data to send a POST request to an API endpoint using Oauth2Session (requests_oauthlib)?

How to send a post request to the Spring controller using ajax

How to send file using ajax post request in django

Ajax request is not sending data over POST

How to send each string object in an array as ajax post data

Why can't I send `None` as data in a POST request using Python's `requests` library?

How can I send "special" data in a POST request using PHP

How to send form data in post request in iOS using MonoTouch?

How to send data in request body with a GET when using jQuery $.ajax()

How to send data to post method of node js using AJAX/jQuery?

How to send XML POST requests using JMeter

How to send a POST Request withou ajax

How to send form with ajax post request with rails

How to send POST request using Python-Requests to a Java Serverlet Endpoint

AJAX Post Request over VPN stripping request data

Send POST request with JSON data using Volley

Send data to the server using POST request with jQuery

Send multiple Post requests using ajax on button click in the same page

Using POST via AJAX and PHP to send data

Promises.all not fired after looping through multiple ajax requests

Send html form data to php using post request with ajax to a https://foo.com

Using requests to send byte-array to webservice using http post

How to send json array as post request in volley?