I need to do sequential asynchronous ajax requests with limited threads. At the moment, I am only allowed to occupy one thread on the web server, so I can only execute one ajax request.
I have the following function that helps me when I am allowed to use only one thread at a time.
function initiateChain() {
var i = 0;
var tasks = arguments;
var callback = function () {
i += 1;
if (i != tasks.length) {
tasks[i](callback);
}
}
if (tasks.length != 0) {
tasks[0](callback);
}
}
Tell me if I have three helper functions ajax
function getGadgets(callback) {
callback();
}
function getBooks(callback) {
callback();
}
function getDeals(callback) {
callback();
}
the next call guarantees that no more than 1 ajax request will be made from this client
initiateChain(getGadgets, getBooks, getDeals);
Now I need to strengthen initiateChain to support an arbitrary number of threads. Let's say I am allowed to use 2 or n number of threads that I would like to know to do this without changing the helper functions ajax getGadgets, getDeals, getDeals.
, , N, getGadgets, getDeals getDeals (| N | = 3), -. , initiateChain . M-, | N | ( M).