You can use .reduce() to iterate the array and jQuery ajax promises to sequence the function calls:
[1, 2, 3].reduce(function(p, item) {
return p.then(function() {
return gear(item);
});
}, $.Deferred().resolve()).then(function() {
// everything done here
});
function gear(params) {
var values = {1: 'abc', 2: 'cba', 3: 'acb'};
return query(values[params]);
}
function query(settings) {
return $.ajax({
...
settings
...
})
}
This will sequence the ajax calls so the next one doesn't start until the prior one is done.
In both gear() and query(), you return the promise that $.ajax() already returns.
Then, you use .reduce() to iterate the array where you accumulate a promise. You pass in an initially resolved promise and then each item from the array adds a .then() onto the end of the chain.
If you wanted to run all the Ajax calls at the same time and then just get a notification when they are all done, you could do this:
$.when.apply($, [1, 2, 3].map(function(item) {
return gear(item);
})).then(function(r1, r2, r3) {
// everything done here
// results are in arguments[0], arguments[1], ... arguments[n]
});
function gear(params) {
var values = {1: 'abc', 2: 'cba', 3: 'acb'};
return query(values[params]);
}
function query(settings) {
return $.ajax({
...
settings
...
})
}
$.eachin a function and pass a callback function to it and call it once each is over