I'm trying to call this API with the request module about 200-300 times with a Lambda function. I need to add second between each call so I don't get a 429 response. I've tried a few different ways to make this happen, but it seems to ignore the code to slow it down.
How do people normally slow down these requests in AWS lambda? It would be great if I could insert something like utilities.sleep(1000) in the loop to make it wait a second before continuing. I'm sure there is a simple solution to this issue, but all the examples I've seen seem to make it complex.
function findProjects(items){
var toggleData = [];
for( var i = 0; i < items.length; i++ ){
setTimeout( callToggle( items[i] ), 1000 );
}
function callToggle( data ){
request({
'url': 'https://www.toggl.com/api/v8/projects/' + data.toggle.data.id,
'method': 'GET',
'headers': {
'Content-Type': 'application/json',
'Accept': 'application/json'
},
'auth': {
'user': 'xxxxxxx',
'pass': 'api_token'
}}, function( error, response, body ){
if( error ) {
console.log( error );
context.done( null, error );
} else {
console.log(response.statusCode, "toggle projects were listed");
var info = JSON.parse(body);
toggleData.push(info);
}
});
}
findDocument( toggleData );
}