How to make it so that I can execute say 10 promises at a time in javascript to prevent rate limits on api calls?

前端 未结 2 1822
醉酒成梦
醉酒成梦 2021-01-24 22:13

I have 1000 records that need to hit an API endpoint that is rate limited. I want to make it so that there is only 5 calls on the URL at any given time so that I am not making 1

2条回答
  •  被撕碎了的回忆
    2021-01-24 22:54

    Use waterfall pattern without a library and use a race condition to resolve on each iteration with reduce. And you can limit the number of calls by specifying the length of the array in Array.from.

    var promise = Array.from({ length: 5 }).reduce(function (acc) {
      return acc.then(function (res) {
        return run().then(function (result) {
          res.push(result);
          return res;
        });
      });
    }, Promise.resolve([]));
    
    
    var guid = 0;
    function run() {
      guid++;
      var id = guid;
      return new Promise(resolve => {
        // resolve in a random amount of time
        setTimeout(function () {
          console.log(id);
          resolve(id);
        }, (Math.random() * 1.5 | 0) * 1000);
      });
    }

提交回复
热议问题