I'm looping through an array and making an API call for each member using async/await, I then push the result into another array which is returned.
// My current function
async requestForEach(repos) {
const result = [];
for (const repo of repos) {
result.push(await this.doSomething(repo.name));
}
return result;
}
// doSomething()
const AWS = require('aws-sdk');
const codecommit = new AWS.CodeCommit();
async doSomething(repoName){
return (await codecommit.listBranches({
repoName
}).promise()).branches;
}
My issue is I'm getting rate limited. If I catch and print the error I get..
ThrottlingException: Rate exceeded {
// Call stack here
code: 'ThrottlingException',
time: 2020-08-16T15:52:56.632Z,
requestId: '****-****-****-****-****',
statusCode: 400,
retryable: true
}
Documentation for the API I'm using can be found here - https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/CodeCommit.html#listBranches-property
I looked into options and this async library seemed to be the popular option.
Using async.queue()..
Tasks added to the queue are processed in parallel (up to the concurrency limit). If all workers are in progress, the task is queued until one becomes available. Once a worker completes a task, that task's callback is called.
// create a queue object with concurrency 2 var q = async.queue(function(task, callback) { console.log('hello ' + task.name); callback(); }, 2);
Obviously I cant get the value back from within the callback function, so how should I approach this problem?