Chetan Purohit Chetan Purohit - 1 year ago 74
Node.js Question

How to make async API calls with rate limit for a particular timespan?

I am working on project in which I have to make almost 500+ calls in one go from my NodeJS server to third party server. The problem is that third party server has a limit of maximum 50 calls in a second. Can you please help me to implement the logic for making only specified number of calls in one second. The technology/language used in my project is NodeJS with HapiJS framework.

Thank you in advance.


You might use async.js eachlLimit with desired delay and simultaneous connections. It would be like:

async.eachLimit(['ur11','ur12','ur13' /*and so on*/], 50/* how many simultaneous requests*/,  function (url, callback) {
    request(url, /*params*/ function(err, result){
        //handle err and result
        setTimeout(callback, 100 /*you desired delay*/)  

}, function (err) {
    // all requests done