Anand Sunderraman
Anand Sunderraman

Reputation: 8148

Async each introduce a delay

Here is my problem.

I have an array of objects, I need to loop through them and make an api request for each object.

My initial option was to use the async each. The problem here is the api cannot accepts say more that 10 requests per second. I was wondering if there was a way to introduce some delay or some strategy where we could restrict the # of api call we make per second

Upvotes: 1

Views: 779

Answers (1)

Ben
Ben

Reputation: 5074

In your case, you can use async.eachLimit() to throttle how many requests you want to be processed at a time.

// Assume documents is an array of JSON objects and requestApi is a
// function that interacts with a rate-limited REST api.

async.eachLimit(documents, 5, requestApi, function(err){

});

Upvotes: 2

Related Questions