I'm trying to make a series of calls to a web api, but the api is complaining about too many calls all at once, so I'm trying to set a delay. Here is some test code that I'm using:
for(var i = 1; i <= 165; i++)
{
var partitionkey = '["' + i + '"]';
const options = {
url: 'https://mytech-lounge-metrics.documents.azure.com/dbs/metrics/colls/LoungeVisits/sprocs/calculateAverage',
method: 'POST',
headers: {
'Authorization': 'authString',
'x-ms-version': '2017-02-22',
'x-ms-date': 'today',
'Content-Type': 'application/json',
'x-ms-documentdb-partitionkey': partitionkey
}
};
setTimeout(function(){
// Some Web API call would theoretically go in here
console.log("Trying for partition " + partitionkey);
}, i*100);
}
As I expected, the loop runs all the way through before the first timeout occurs, and, due to JS scoping rules, the output is:
Trying for partition ["165"]
Trying for partition ["165"]
Trying for partition ["165"]
Trying for partition ["165"]
Trying for partition ["165"]
Trying for partition ["165"]
Trying for partition ["165"]
Trying for partition ["165"]
...
How can I put a 100ms delay between each of my calls to the webapi in this loop while retaining the values I want to send in the headers, ie ["1"]
, ["2"]
, etc?