0

Below is an AWS Lambda function (node.js) that queries DynamoDB and then hits an API for additional data from the response. The problem is that the particular API only allows 4 calls per second, therefore the response is split into chunks of 4. How can the for loop wait to receive each response from the chunk before moving onto the next chunk to not exceed the API's limit? In other words, how do you throttle a for loop with an API call within, to wait 1 second before moving onto the next chunk of 4 calls?

exports.handler = (event, context, callback) => {
  const params = {
    ...
  };
  documentClient.query(params, (err, data) => {
      if (err) {
          console.log(err);
          callback(err);
      } else {
          const items = [];
          const chunkSize = 4; // API allows 4 calls per second
          for (let i = 0; i < data.Items.length; i += chunkSize) {
              const chunk = data.Items.slice(i, i + chunkSize);
              const itemChunk = chunk.map(item => {
                  let obj = {
                      ...
                  };
                  axios.get('xxxxx')
                    .then(res => {
                        ...
                    }).catch(err => {
                        console.log(err)
                    });
                    return obj;
              });
              items.push(itemChunk);
          }
          const flattenedItems = items.flat();
          const response = {
              statusCode: 200,
              body: flattenedItems,
          };
          callback(null, response);
      }
  });
}
ckingchris
  • 559
  • 1
  • 12
  • 25
  • The bottom of [this answer](https://stackoverflow.com/a/66510044/6243352) shows a chunking example with promises. `return axios.get()` and use `Promise.all` to wait for the promises that comprise the chunk, then sleep with a promisified timeout for a second to throttle. A throttled queue would be more effiicient. It's possible to use callbacks all the way through but also... why? – ggorlen Aug 04 '22 at 05:36
  • personally I'd do a request every 250ms - chunking doesn't really impact perceived performance in this case ... I mean, if the limit was expressed as 240/minute, then of course you'd benefit from chunking when you have less than 240 requests - but that's just my opinion – Jaromanda X Aug 04 '22 at 06:11
  • @JaromandaX that would have the same effect and be less cumbersome, i'll give it a try – ckingchris Aug 04 '22 at 16:06

1 Answers1

0

After further research I came across this node.js library that makes it very simple to throttle API requests, I hope this helps others looking to throttle API calls in node.js

https://github.com/jhurliman/node-rate-limiter

ckingchris
  • 559
  • 1
  • 12
  • 25