Home > Software engineering >  How do you throttle a Javascript for loop with an API call within, in chunks of 4 calls per second?
How do you throttle a Javascript for loop with an API call within, in chunks of 4 calls per second?

Time:08-07

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);
      }
  });
}

CodePudding user response:

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

  • Related