Rate limit NodeJS bluebird Promise.map

Viewed 118

I have to call a 3rd party API repeatedly using bluebird Promise.map API in nodeJS. But the problem is the API supports only 20 requests per second. I wanted understand how this can be implemented. This is what I tried. But it gives me an infinite loop.

let count = 0;
setInterval(() => count = 0, 1000); //reset the counter to 0 once in every second

await Promise.map(data, datum => {
  count++;

  while(count>=20){ //create an infinite loop if the count is more than 20 to wait till the count is reset. 
  }                 //The loop exits once the count value is reset to 0 by setIntercal
   
  await axios({
    url: process.env.MDNFS_URL,
    body: datum,
    method: 'POST'
  })
}, {concurrency:10})

1 Answers

I omitted bluebird, but you could pretty much do the exact same thing with it.

const { promisify } = require('util');
const wait = promisify(setTimeout);

function chunk(arr, size) {
  return Array.from(Array(Math.ceil(arr.length / size)), (el, i) => arr.slice(i * size, i * size + size));
}

for (let dataChunk of chunk(data, 20)) {
  await Promise.all(dataChunk.map((datum) => axios({
    url: process.env.MDNFS_URL,
    body: datum,
    method: 'POST'
  })).concat(wait(1000)))
}

Related