I am creating my own JavaScript application that loads Ajax calls into the same process at a specific time. Instead of going through a regular loop and doing everything at once, I thought I would wait for the Ajax call to complete, and then do the following.
Using Stackoverflow, I was able to do this as follows:
function ajaxCall(index) { return new Promise(function(resolve) { // Ajax request { resolve(); // } }); } Promise.resolve(0).then(function loop(i) { if (i < length) { return ajaxCall(i).thenReturn(i + 1).then(loop); } }).then(function() { // for loop complete }); Promise.prototype.thenReturn = function(value) { return this.then(function() { return value; }); };
However , this is too slow for me. I want to be able to store a var that keeps track of how many Ajax calls are currently in progress so that I can limit the number.
I tried several things, but I continue to work in endless cycles or do not get the desired result.
How to make a few number-limited asynchronous Ajax calls using the Promise for loop?
source share